Python GUI编程--Tkinter
??? 今天看到了GUI編程,書上推薦用wxPython,去官網(wǎng)上看了看,發(fā)現(xiàn)Windows的最高支持到2.7,我用的是3.4版本,咋辦,用自帶的庫--Tkinter唄,它是Python的默認(rèn)GUI庫,幾乎是個(gè)標(biāo)準(zhǔn)庫,也是受廣大開發(fā)者喜愛的。
??? Tkinter有很多組件(其實(shí)也不多,十來個(gè)),今天主要用標(biāo)簽、按鈕、進(jìn)度條,寫一個(gè)字體隨進(jìn)度條改變大小并且可以寫文件的小程序,其他組件用法看文檔就行,以前用C#寫過winform的應(yīng)該有經(jīng)驗(yàn)。直接上代碼:
#---coding:utf-8--- from tkinter import * #導(dǎo)包 def resize(ev=None):'根據(jù)進(jìn)度條調(diào)整字體大小'label.config(font='Helvetica -%d bold' %scale.get())def writefile():'寫文件'try:f = open(r'd:\hello.txt','w')f.write('hello,world!')except Exception as e:print(e)finally:f.close()top = Tk()#新建一個(gè)窗口 top.geometry('400x300')#指定窗口大小 top.title('GUI_test')label = Label(top,text='Hello,World!',font='Helvetica -12 bold')#隨進(jìn)度條變化的標(biāo)簽,剛開始學(xué)當(dāng)然用hello,world label.pack(fill=Y,expand=1)scale = Scale(top,from_=10,to=50,orient=HORIZONTAL,command=resize)#進(jìn)度條,個(gè)人認(rèn)為command作用和綁定差不多 scale.set(12)#設(shè)初值 scale.pack(fill=X,expand=1)write = Button(top,text="Write",command=writefile) write.pack()quit = Button(top,text="Quit",command=top.quit,activeforeground='White',activebackground='red') quit.pack()mainloop()#調(diào)用該函數(shù)運(yùn)行程序??? ?注意到每配置好一個(gè)組件,后面都有一句X.pack(***),最后的mainloop()是用來啟動你編的GUI程序,那pack()是什么鬼,查一下文檔,有這么一段話
The packer is one of Tk’s geometry-management mechanisms. Geometry managers are used to specify the relative positioning of the positioning of widgets within their container - their mutual master. In contrast to the more cumbersome placer (which is used less commonly, and we do not cover here), the packer takes qualitative relationship specification - above, to the left of, filling, etc - and works everything out to determine the exact placement coordinates for you.The pack() method can be called with keyword-option/value pairs that control where the widget is to appear within its container, and how it is to behave when the main application window is resized.意思大概就是說packer這哥們是管理和顯示組件的,pack()方法用來指定組件的顯示??聪逻\(yùn)行效果:
???
??? 打開D盤hello.txt,發(fā)現(xiàn)了我們寫的hello,world!這樣簡單的GUI編程完成了,還用到了一點(diǎn)文件編程的知識,和預(yù)期效果一樣,得趕快學(xué)習(xí)網(wǎng)絡(luò)部分了,不然畢設(shè)進(jìn)度得耽擱了。
轉(zhuǎn)載于:https://www.cnblogs.com/littleseven/p/5369149.html
總結(jié)
以上是生活随笔為你收集整理的Python GUI编程--Tkinter的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java语言基础(数组)
- 下一篇: websocket python爬虫_p