600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > GUI 图形用户界面编程实例-文件对话框获取文件

GUI 图形用户界面编程实例-文件对话框获取文件

时间:2018-10-25 06:08:54

相关推荐

GUI 图形用户界面编程实例-文件对话框获取文件

✨✨✨

感谢优秀的你打开了小白的文章

“希望在看文章的你今天又进步了一点点,离美好生活更近一步!”🌈

filedialog

是文件对话框,在程序运行该过程中,当你需要手动选择文件或手动选择文件存储路径时,就需要用到tkinter库中filedialog提供的函数。

函数及用法

filedialog.askopenfilename(***options)filedialog.askopenfilenames(**options)filedialog.asksaveasfile(**options)filedialog.askdirectory(**options)

filedialog.askopenfilename(**options)

自动打开选取窗口,手动选择一个文件,返回文件路径,类型为字符串。

可选参数:title、filetypes、initialdir、multiple

filedialog.askopenfilenames(**options)

同时选择多个文件,返回一个元组,包括所有选择文件的路径。

可选参数:title、filetypes、initialdir

filedialog.asksaveasfile(**options)

选择文件存储路径并命名,可选参数:title、filetypes、initialdir、efaultextension

如果 filetypes=[(“文本文档”, “.txt”)] ,可以不写文件扩展名,扩展名自动为txt;

如果 *filetypes=[(‘All Files’, ’ ')] ,一定写文件扩展名,否则无扩展名;

如果 filetypes=[(“文本文档”, “.txt”)] ,efaultextension=‘.tif’,可以不写文件扩展名,扩展名自动为tif。

filedialog.askdirectory(**options)

选择一个文件夹,返回文件夹路径。

可选参数:title、initialdir

基本案例-选择视频文件

from tkinter import *from tkinter.filedialog import *root = Tk();root.geometry("400x100")def test1():f = askopenfilename(title="上传文件",initialdir="f:",filetypes=[("视频文件",".mp4")])show["text"]=fButton(root,text="选择编辑的视频文件",command=test1).pack()show = Label(root,width=40,height=3,bg="pink")show.pack()root.mainloop()

打开后即可选择:

延展-打开excel文件

from tkinter import *from tkinter import filedialogimport tkinter.messageboxdef main():def selectExcelfile():sfname = filedialog.askopenfilename(title='选择Excel文件', filetypes=[('Excel', '*.xlsx'), ('All Files', '*')])print(sfname)text1.insert(INSERT,sfname)def closeThisWindow():root.destroy()def doProcess():tkinter.messagebox.showinfo('提示','处理Excel文件的示例程序。')#初始化root=Tk()#设置窗体标题root.title('Python GUI Learning')#设置窗口大小和位置root.geometry('500x300+570+200')label1=Label(root,text='请选择文件:')text1=Entry(root,bg='white',width=45)button1=Button(root,text='浏览',width=8,command=selectExcelfile)button2=Button(root,text='处理',width=8,command=doProcess)button3=Button(root,text='退出',width=8,command=closeThisWindow)label1.pack()text1.pack()button1.pack()button2.pack()button3.pack() label1.place(x=30,y=30)text1.place(x=100,y=30)button1.place(x=390,y=26)button2.place(x=160,y=80)button3.place(x=260,y=80)root.mainloop() if __name__=="__main__":main()

增加浏览,处理与退出按钮,更加方便使用:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。