python GUI编程tkinter(2)

作者: Lykit01 | 来源:发表于2019-02-17 12:57 被阅读2次

上次讲了基本的控件,这次做了一个小实践。

1.问题描述

我们要做一个UI界面,读取两个文件,然后对文件做出分析,最后保存到指定文件夹。

2.程序实现

(1)主循环

root=Tk()
root.title("心愿单日报生成器")
root.geometry("400x100")
wishlistname=""
wishlistvisitname=""
data_ready=False
save_path=""
   
file_lb=Label(root,text='')
file_lb.place(x=200,y=50,anchor="center")
choose_file_btn=Button(root,text="选择源文件",command=choose_files)
choose_file_btn.place(x=50,y=80,anchor="center")
execute_btn=Button(root,text="选择日报保存路径",command=execute)
execute_btn.place(x=150,y=80,anchor="center")

#提示分析中间的问题
note_lb=Label(root,text="请选择2个源文件:心愿单数据和访问数据。")
note_lb.place(x=200,y=10,anchor="center")

#保存路径
path=StringVar()
Entry(root,textvariable=path).place(x=300,y=80,anchor="center")

root.mainloop()

这里没用pack,而是用了place来调节具体位置,以使界面更好看

(2)选取文件

这个要用到tkinter.filedialog.askopenfilenames()这个函数。在开头要import tkinter.filedialog

#选择源文件
def choose_files():
    #每次点击选择源文件,都会清空之前的选择,并且把wishlistname、data_ready等清空
    file_lb.config(text="")
    global wishlistname
    global wishlistvisitname
    global data_ready
    wishlistname=""
    wishlistvisitname=""
    data_ready=False
    
    filenames=tkinter.filedialog.askopenfilenames()
        ###中间省略具体的文件名判定过程###
    file_lb.config(text="您选择的文件是:"+filenames_str)
    note_lb.config(text=lb_text)

(3)选择保存路径(文件夹)

这个操作要用到函数tkinter.filedialog.askdirectory()

def select_path():
    global save_path
    path_=tkinter.filedialog.askdirectory()
    path.set(path_)

注意这里的path是textvariable变量,改变之后Entry里也会相应的更改。

最后看一下成品效果!


简单的效果

相关文章

网友评论

    本文标题:python GUI编程tkinter(2)

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