如何更改的文本,label_1
使其等于在其中选择的文件browse_for_file_1
?我一直在尝试各种方法,但似乎无法更新GUI。我认为可能是因为它在一个框架内?
import Tkinter as tk
import tkFileDialog
root = tk.Tk()
#Frames
frame_1 = tk.Frame(root)
frame_1.pack()
def browse_for_file_1():
file_name_1 = tkFileDialog.askopenfilename(parent=root,title='Open 1st File')
print file_name_1
label_1.config(text=file_name_1)
root.update()
#Browse 1
browse_button_1 = tk.Button(frame_1, text='Browse for 1st File', width=25, command=browse_for_file_1).pack(side=tk.LEFT, pady=10, padx=10)
label_1 = tk.Label(frame_1, fg="red", text="No file selected.")
label_1.pack(side=tk.RIGHT, pady=10, padx=10)
#Quit Button
quit = tk.Button(root, text='QUIT', width=25, fg="red", command=root.destroy).pack(pady=10, padx=10)
root.title("Zero Usage")
root.mainloop()
更改通话:
browse_button_1 = tk.Button(frame_1, text='Browse for 1st File', width=25, command=lambda:browse_for_file_1(label_1)).pack(side=tk.LEFT, pady=10, padx=10)
然后您的函数可以是:
def browse_for_file_1(label_1):
file_name_1 = tkFileDialog.askopenfilename(parent=root,title='Open 1st File')
label_1.config(text=file_name_1)
# or label_1.config({'text':file_name_1})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句