Python GUI編程完整示例
本文實(shí)例講述了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 + ':未找到文件,請(qǐng)檢查路徑!'
elif not os.path.isdir(tdir):
error = tdir + ':不是一個(gè)路徑!'
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入門(mén)與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- 簡(jiǎn)單介紹利用TK在Python下進(jìn)行GUI編程的教程
- python3.6 +tkinter GUI編程 實(shí)現(xiàn)界面化的文本處理工具(推薦)
- Python Tkinter GUI編程入門(mén)介紹
- Python GUI編程 文本彈窗的實(shí)例
- python GUI編程(Tkinter) 創(chuàng)建子窗口及在窗口上用圖片繪圖實(shí)例
- Python中使用Tkinter模塊創(chuàng)建GUI程序?qū)嵗?/a>
- Python Tkinter模塊 GUI 可視化實(shí)例
- Python GUI Tkinter簡(jiǎn)單實(shí)現(xiàn)個(gè)性簽名設(shè)計(jì)
- python tkinter GUI繪制,以及點(diǎn)擊更新顯示圖片代碼
- Python GUI編程學(xué)習(xí)筆記之tkinter界面布局顯示詳解
相關(guān)文章
Python 找出英文單詞列表(list)中最長(zhǎng)單詞鏈
這篇文章主要介紹了Python 找出英文單詞列表(list)中最長(zhǎng)單詞鏈,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Python發(fā)送郵件測(cè)試報(bào)告操作實(shí)例詳解
這篇文章主要介紹了Python發(fā)送郵件測(cè)試報(bào)告操作,結(jié)合實(shí)例形式較為詳細(xì)的分析了Python郵件發(fā)送相關(guān)模塊使用及操作注意事項(xiàng),需要的朋友可以參考下2018-12-12
python必備庫(kù)Matplotlib畫(huà)圖神器
這篇文章主要介紹了python必備庫(kù)Matplotlib畫(huà)圖神器,Matplotlib 是 Python 中最受歡迎的數(shù)據(jù)可視化軟件包之一,支持跨平臺(tái)運(yùn)行,它是 Python 常用的 2D 繪圖庫(kù),同時(shí)它也提供了一部分 3D 繪圖接口,更多詳細(xì)內(nèi)容,需要的小伙伴可以參考一下下面文章具體內(nèi)容2022-03-03
pytorch基礎(chǔ)之損失函數(shù)與反向傳播詳解
損失函數(shù)(Loss?Function)用于衡量神經(jīng)網(wǎng)絡(luò)輸出與目標(biāo)值之間的誤差,指導(dǎo)網(wǎng)絡(luò)通過(guò)反向傳播優(yōu)化參數(shù),常見(jiàn)的損失函數(shù)包括均方誤差和交叉熵誤差,在訓(xùn)練過(guò)程中,通過(guò)不斷最小化損失函數(shù)值來(lái)調(diào)整網(wǎng)絡(luò)權(quán)重,以期達(dá)到輸出接近目標(biāo)值的效果2024-09-09
在雙python下設(shè)置python3為默認(rèn)的方法
這篇文章主要介紹了如何在雙python下設(shè)置python3為默認(rèn),本文通過(guò)一個(gè)例子分步驟給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-10-10
使用Python實(shí)現(xiàn)畫(huà)一個(gè)中國(guó)地圖
今天小編就為大家分享一篇使用Python實(shí)現(xiàn)畫(huà)一個(gè)中國(guó)地圖,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11

