1 from tkinter import * 2 from tkinter.filedialog import * 3 4 filetype = [(‘Python Files‘, ‘*.py *.pyw‘), 5 (‘Text Files‘, ‘*.txt‘), 6 (‘All Files‘, ‘*.*‘)] 7 8 def saveFileDialog(): 9 "保存对话框" 10 filename = asksaveasfilename( 11 #默认扩展名,.号可带可不带 12 defaultextension = ‘.py‘, 13 #文件类型选项 14 filetypes = filetype, 15 #初始目录,默认当前目录 16 initialdir = ‘G:\\Tkinter‘, 17 #初始文件名,默认为空 18 initialfile = ‘Test‘, 19 #打开的位置,默认是根窗口 20 parent = root, 21 #窗口标题 22 title = "另存为") 23 print(filename) 24 25 def openFileDialog(): 26 "打开对话框,参数与保存对话框相同.略" 27 filename = askopenfilename(filetypes = filetype) 28 print(filename) 29 30 root = Tk() 31 menubar = Menu(root) 32 filemenu = Menu(menubar, tearoff = 0) 33 filemenu.add_command(label = ‘打开‘, command = openFileDialog) 34 filemenu.add_command(label = ‘保存‘, command = saveFileDialog) 35 menubar.add_cascade(label = ‘文件‘, menu = filemenu) 36 root[‘menu‘] = menubar 37 root.title(‘文件对话框‘) 38 root.mainloop()
时间: 2024-10-31 14:27:27