Python GUI編程完整示例
本文實例講述了Python GUI編程。分享給大家供大家參考,具體如下:
import os from time import sleep from tkinter import * from tkinter.messagebox import showinfo class DirList(object): def __init__(self, initdir=None): self.top = Tk() self.label = Label(master=self.top, text='Directory Lister V1.0') self.label.pack() self.cwd = StringVar(master=self.top) self.dirl = Label(self.top, fg='blue', font=('Helvetica', 14, 'bold')) self.dirl.pack() self.dirfm = Frame(master=self.top) self.dirsb = Scrollbar(master=self.dirfm) self.dirsb.pack(side=RIGHT,fill=Y) # fill=Y,垂直填充空間排列 self.dirs = Listbox(master=self.dirfm, height=15, width=50, yscrollcommand=self.dirsb.set) self.dirs.bind('<Double-1>', func=self.setDirAndGo) # <Double-1>,雙擊顯示路徑列表 self.dirsb.config(command=self.dirs.yview) self.dirs.pack(side=LEFT, fill=BOTH) self.dirfm.pack() self.dirn = Entry(master=self.top, width=50, textvariable=self.cwd) self.dirn.bind('<Return>', func=self.doLS) self.dirn.pack() self.bfm = Frame(master=self.top) self.cleer = Button(master=self.bfm, text='清除', command=self.clrDir, activeforeground='white', activebackground='blue') self.ls = Button(master=self.bfm, text='顯示列表', command=self.doLS, activeforeground='white', activebackground='green') self.quit = Button(master=self.bfm, text='退出', command=self.top.quit, activeforeground='white', activebackground='red') self.cleer.pack(side=LEFT) self.ls.pack(side=LEFT) self.quit.pack(side=LEFT) self.bfm.pack() if initdir: self.cwd.set(os.curdir) self.doLS() def setDirAndGo(self, ev=None): self.last = self.cwd.get() self.dirs.config(selectbackground='red') chek = self.dirs.get(self.dirs.curselection()) if not chek: chek = os.curdir self.cwd.set(chek) self.doLS() def doLS(self, ev=None): error = '' tdir = self.cwd.get() if not tdir: tdir = os.curdir if not os.path.exists(tdir): error = tdir + ':未找到文件,請檢查路徑!' elif not os.path.isdir(tdir): error = tdir + ':不是一個路徑!' if error: # self.cwd.set(error) showinfo(title='提示',message=error) self.top.update() # sleep(2) if not (hasattr(self, 'last') and self.last): self.last = os.curdir self.cwd.set(self.last) self.dirs.config(selectbackground='LightSkyBlue') self.top.update() return if not os.path.isdir(tdir): self.cwd.set('') else: self.cwd.set('獲取目錄內(nèi)容中...') self.top.update() dirlist = os.listdir(tdir) dirlist.sort() os.chdir(tdir) self.dirl.config(text=os.getcwd()) self.dirs.delete(0, END) self.dirs.insert(END, os.curdir) self.dirs.insert(END, os.pardir) for eachfile in dirlist: self.dirs.insert(END, eachfile) self.cwd.set(os.curdir) self.dirs.config(selectbackground='LightSkyBlue') def clrDir(self, ev=None): self.cwd.set('') if __name__ == '__main__': dir = DirList(os.curdir) mainloop()
效果如下:
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
- 簡單介紹利用TK在Python下進(jìn)行GUI編程的教程
- python3.6 +tkinter GUI編程 實現(xiàn)界面化的文本處理工具(推薦)
- Python Tkinter GUI編程入門介紹
- Python GUI編程 文本彈窗的實例
- python GUI編程(Tkinter) 創(chuàng)建子窗口及在窗口上用圖片繪圖實例
- Python中使用Tkinter模塊創(chuàng)建GUI程序?qū)嵗?/a>
- Python Tkinter模塊 GUI 可視化實例
- Python GUI Tkinter簡單實現(xiàn)個性簽名設(shè)計
- python tkinter GUI繪制,以及點擊更新顯示圖片代碼
- Python GUI編程學(xué)習(xí)筆記之tkinter界面布局顯示詳解
相關(guān)文章
python靜態(tài)web服務(wù)器實現(xiàn)方法及代碼詳解
在本篇內(nèi)容里小編給大家分享了一篇關(guān)于python靜態(tài)web服務(wù)器實現(xiàn)方法,有需要的朋友們可以參考下。2022-11-11Python實現(xiàn)微信好友數(shù)據(jù)爬取及分析
這篇文章會基于Python對微信好友進(jìn)行數(shù)據(jù)分析,這里選擇的維度主要有:性別、頭像、簽名、位置,主要采用圖表和詞云兩種形式來呈現(xiàn)結(jié)果,其中,對文本類信息會采用詞頻分析和情感分析兩種方法,感興趣的小伙伴可以了解一下2021-12-12Pycharm中出現(xiàn)ImportError:DLL load failed:找不到指定模塊的解決方法
這篇文章主要介紹了Pycharm中出現(xiàn)ImportError:DLL load failed:找不到指定模塊的解決方法,需要的朋友可以參考下2019-09-09Python實現(xiàn)查看系統(tǒng)啟動項功能示例
這篇文章主要介紹了Python實現(xiàn)查看系統(tǒng)啟動項功能,涉及Python針對系統(tǒng)注冊表啟動項的相關(guān)讀取操作實現(xiàn)技巧,需要的朋友可以參考下2018-05-05python中如何正確使用正則表達(dá)式的詳細(xì)模式(Verbose mode expression)
許多程序設(shè)計語言都支持利用正則表達(dá)式進(jìn)行字符串操作,python自然也不例外,下面這篇文章主要給大家介紹了關(guān)于在python中如何正確使用正則表達(dá)式的詳細(xì)模式(Verbose mode expression)的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-11-11Python實現(xiàn)簡單的文件傳輸與MySQL備份的腳本分享
這篇文章主要介紹了Python實現(xiàn)簡單的文件傳輸與MySQL備份的腳本分享,用到了socket與tarfile模塊,需要的朋友可以參考下2016-01-01