文件对话框简单示例二

文件对话框简单示例二

代码

import tkinter as tk
import tkinter.filedialog

root = tk.Tk()
root.title('测试颜色选择框')
root.geometry('500x400+400+200')
root.resizable(width=0, height=0)


def file_path():
    with tk.filedialog.askopenfile(initialdir=r'G:\tmp\test', title='文件', filetypes=[('文本文件', '.txt')]) as f:
        text.insert(1.0, f.read())


btn = tk.Button(root, text='请选择文件', width=20, height=2, command=file_path)
btn.pack()

text = tk.Text(root, bg='white', width=70, height=27)
text.pack()
root.mainloop()

结果展示

posted @ 2021-04-17 20:54  廿九九  阅读(42)  评论(0)    收藏  举报