Python GUI編程完整示例
更新時間:2019年04月04日 10:42:20 作者:薔薇Nina
這篇文章主要介紹了Python GUI編程,結(jié)合完整示例形式分析了Python基于tkinter模塊的GUI圖形界面編程相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
本文實例講述了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īng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
您可能感興趣的文章:
- 簡單介紹利用TK在Python下進行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)文章
pytorch基礎(chǔ)之損失函數(shù)與反向傳播詳解
損失函數(shù)(Loss?Function)用于衡量神經(jīng)網(wǎng)絡(luò)輸出與目標值之間的誤差,指導(dǎo)網(wǎng)絡(luò)通過反向傳播優(yōu)化參數(shù),常見的損失函數(shù)包括均方誤差和交叉熵誤差,在訓(xùn)練過程中,通過不斷最小化損失函數(shù)值來調(diào)整網(wǎng)絡(luò)權(quán)重,以期達到輸出接近目標值的效果2024-09-09

