python Windows tkinter应用开发1创建初始页

作者: python测试开发 | 来源:发表于2018-12-10 22:27 被阅读7次
图片.png

欢迎来到python Windows tkinter应用开发第一章,我们将开始创建一个应用程序,它删除计算机文件夹中的重复文件。该项目将使用tkinter模块,它是python 3的标准库。

第一章非常简单,我们将创建一个按钮,当点击时它将打开文件选择器窗口,允许我们从文件夹中选择文件。

本教程要求具备python基础。

代码:

from tkinter import *
from tkinter import filedialog

win = Tk() # 1 Create instance
win.title("Multitas") # 2 Add a title
win.resizable(0, 0) # 3 Disable resizing the GUI

# 4 Create a label
aLabel = Label(win, text="Remove duplicate file")
aLabel.grid(column=0, row=0) # 5 Position the label

# 6 Create a selectFile function to be used by button
def selectFile():

    filename = filedialog.askopenfilename(initialdir="/", title="Select file")
    print(filename) # 7 print the filename from the selected file

# 8 Adding a Button
action = Button(win, text="Search File", command=selectFile)
action.grid(column=0, row=1) # 9 Position the button

win.mainloop()  # 10 start GUI

执行:

image

选择文件之后,会在打印出选择的文件名:

参考资料

image

相关文章

网友评论

    本文标题:python Windows tkinter应用开发1创建初始页

    本文链接:https://www.haomeiwen.com/subject/rrljhqtx.html