Python下用Tkinter進行GUI編程


    Python可用的GUI編程的包很多,Tkinter也是其中一個半標准的工具包。

    作為一個老牌的Python GUI工具包(皮皮書屋里找了本書,竟然是2001年的),它由Tk GUI包裝而來。在Windows版里面已經包括了,不用單獨下載。

    用Tkinter實現一個簡單的GUI程序,單擊click按鈕時會在終端打印出’hello world’:

__author__ = 'fyby'
from Tkinter import *   #引入Tkinter工具包
def hello():
    print('hello world!')

win = Tk()  #定義一個窗體
win.title('Hello World')    #定義窗體標題
win.geometry('400x200')     #定義窗體的大小,是400X200像素

btn = Button(win, text='Click me', command=hello)
#注意這個地方,不要寫成hello(),如果是hello()的話,
#會在mainloop中調用hello函數,
# 而不是單擊button按鈕時出發事件
btn.pack(expand=YES, fill=BOTH) #將按鈕pack,充滿整個窗體(只有pack的組件實例才能顯示)

mainloop() #進入主循環,程序運行

 

image

 

    當我們寫一個較大的程序時,最好將代碼分成一個或者是幾個類,再看一下Hello World例子

#-*- encoding=UTF-8 -*-
__author__ = 'fyby'
from Tkinter import *
class App:
    def __init__(self, master):
        #構造函數里傳入一個父組件(master),創建一個Frame組件並顯示
        frame = Frame(master)
        frame.pack()
        #創建兩個button,並作為frame的一部分
        self.button = Button(frame, text="QUIT", fg="red", command=frame.quit)
        self.button.pack(side=LEFT) #此處side為LEFT表示將其放置 到frame剩余空間的最左方
        self.hi_there = Button(frame, text="Hello", command=self.say_hi)
        self.hi_there.pack(side=LEFT)

    def say_hi(self):
        print "hi there, this is a class example!"

win = Tk()
app = App(win)
win.mainloop()

 

image

    看完了上面兩個無聊的Hello World例子,再來看一個稍微Perfect點的東西吧。Menu組件,自己畫一個像樣點的程序外殼。

#-*- encoding=UTF-8 -*-
__author__ = 'fyby'
from Tkinter import *

root = Tk()

def hello():
    print('hello')

# 創建一個導航菜單
menubar = Menu(root)
menubar.add_command(label="Hello!", command=hello)
menubar.add_command(label="Quit!",command=root.quit)

root.config(menu=menubar)

mainloop()

 

image

        這個程序還是有點無趣,因為我們只是創建了一個頂級的導航菜單,點擊后只是在終端中輸出hello而已,下面來創建一個下拉菜單,這樣才像一個正兒八經的應用大笑在下面的這個例子中,會創建三個頂級菜單,每個頂級菜單中都有下拉菜單(用add_command方法創建,最后用add_cascade方法加入到上級菜單中去),為每個下拉選項都綁定一個hello函數,在終端中打印出hello.

        root.quit是退出這個Tk的實例。

#-*- encoding=UTF-8 -*-
__author__ = 'fyby'
from Tkinter import *
root = Tk()

def hello():
    print('hello')

def about():
    print('我是開發者')

menubar = Menu(root)

#創建下拉菜單File,然后將其加入到頂級的菜單欄中
filemenu = Menu(menubar,tearoff=0)
filemenu.add_command(label="Open", command=hello)
filemenu.add_command(label="Save", command=hello)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)

#創建另一個下拉菜單Edit
editmenu = Menu(menubar, tearoff=0)
editmenu.add_command(label="Cut", command=hello)
editmenu.add_command(label="Copy", command=hello)
editmenu.add_command(label="Paste", command=hello)
menubar.add_cascade(label="Edit",menu=editmenu)
#創建下拉菜單Help
helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label="About", command=about)
menubar.add_cascade(label="Help", menu=helpmenu)

#顯示菜單
root.config(menu=menubar)

mainloop()

 

 

image

  寫了這一些,差不多對Tkinter有了一個大體的印象了。在Python中用Tkinter繪制GUI界面還是蠻簡單的。再把上面的例子擴展一下,和Label標簽結合,當單擊about的時候,在窗體上打印About的內容,而不是在終端輸出。將about函數稍微修改一下。單擊about以后將會調用about函數渲染frame繪制一個標簽並顯示其內容。
def about():
    w = Label(root,text="開發者感謝名單\nfuyunbiyi\nfyby尚未出現的女朋友\nhttp://www.programup.com網站")
    w.pack(side=TOP)

 

image

 

        關於Tkinter更多的內容,參考http://www.programup.com/wiki/beginning_tkinter/,例子原型主要來自於該網站,還有一本書(就是文章開頭提到過的那那本01年的書,http://www.ppurl.com/2012/02/python%E4%B8%8Etkinter%E7%BC%96%E7%A8%8B.html)。還有,國內的書記得《征服Python》中好像也有關於Tkinter的例子,在VeryCD上應該可以找的到大聲笑


免責聲明!

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



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