Python - 文件对话框

打开文件对话框

import tkinter
import tkinter.filedialog

def btn_click():
    global p1
    fn = tkinter.filedialog.askopenfilename(filetypes=[('PNG', '.png')])
    print(fn)
    p1 = tkinter.PhotoImage(file=fn)
    b1.config(image=p1)

main = tkinter.Tk()
main.geometry('300x200')

b1 = tkinter.Button(main, text='...')
b1.config(command=btn_click)
b1.pack()

另存为文件对话框

import tkinter
import tkinter.filedialog

fn = tkinter.filedialog.asksaveasfilename(filetypes=[('TXT', '.txt')])
print(fn)

with open(fn, 'w') as f:
    f.write('abcdefg')
posted @ 2022-07-03 19:00  hgrun  阅读(155)  评论(0编辑  收藏  举报