需求
使用 tkinter 创建一个GUI,GUI 分为两部分,其中一个按钮点击后可选择文件,另一个文本显示窗口将显示文件路径。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| import tkinter as tk from tkinter import filedialog
def open_file_dialog(): file_path = filedialog.askopenfilename() if file_pate: text_var.set(file_path)
root = tk.Tk() root.title("File Selection GUI")
button = tk.Button(root, text="选择文件", command=open_file_dialog) button.pack(pady=10)
text_var = tk.StringVar() text_label = tk.Label(root, textvariable=text_var, wraplength=400) text_label.pack(pady=10)
root.mainloop()
|
运行效果
