Python學習筆記_一個Tkinter示例,使用FileDialog


為了使用Python進行數據分析,編寫一個圖形界面,選擇一個Excel文件(或CSV),然后進行后續處理。

一、本示例涵蓋如下知識點:

1、FileDialog的使用

2、退出程序

3、消息提示框的示例

4、文本框賦值

二、實驗環境:

 1、OS:Win 10 64位

 2、Python 3.7

 

三、進一步說明

1、代碼如下:

from tkinter import *
from tkinter import filedialog
import tkinter.messagebox

def 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()

2、運行后如下圖:

其中,“瀏覽”,“退出”按鈕的功能已編寫,“處理”的功能,僅給出一個消息提示,真正使用,需在此處增加相應的功能。

 

本示例中代碼實測可用。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM