天天看點

Python下用Tkinter進行GUI程式設計 Python下用Tkinter進行GUI程式設計

Python下用Tkinter進行GUI程式設計

    Python可用的GUI程式設計的包很多,Tkinter也是其中一個半标準的工具包。

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

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

Python下用Tkinter進行GUI程式設計 Python下用Tkinter進行GUI程式設計
__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() #進入主循環,程式運作      
Python下用Tkinter進行GUI程式設計 Python下用Tkinter進行GUI程式設計
Python下用Tkinter進行GUI程式設計 Python下用Tkinter進行GUI程式設計

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

Python下用Tkinter進行GUI程式設計 Python下用Tkinter進行GUI程式設計
#-*- 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()      
Python下用Tkinter進行GUI程式設計 Python下用Tkinter進行GUI程式設計
Python下用Tkinter進行GUI程式設計 Python下用Tkinter進行GUI程式設計

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

Python下用Tkinter進行GUI程式設計 Python下用Tkinter進行GUI程式設計
#-*- 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()      
Python下用Tkinter進行GUI程式設計 Python下用Tkinter進行GUI程式設計
Python下用Tkinter進行GUI程式設計 Python下用Tkinter進行GUI程式設計

        這個程式還是有點無趣,因為我們隻是建立了一個頂級的導航菜單,點選後隻是在終端中輸出hello而已,下面來建立一個下拉菜單,這樣才像一個正兒八經的應用

Python下用Tkinter進行GUI程式設計 Python下用Tkinter進行GUI程式設計

在下面的這個例子中,會建立三個頂級菜單,每個頂級菜單中都有下拉菜單(用add_command方法建立,最後用add_cascade方法加入到上級菜單中去),為每個下拉選項都綁定一個hello函數,在終端中列印出hello.

        root.quit是退出這個Tk的執行個體。

Python下用Tkinter進行GUI程式設計 Python下用Tkinter進行GUI程式設計
#-*- 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()      
Python下用Tkinter進行GUI程式設計 Python下用Tkinter進行GUI程式設計
Python下用Tkinter進行GUI程式設計 Python下用Tkinter進行GUI程式設計
  寫了這一些,差不多對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)      
Python下用Tkinter進行GUI程式設計 Python下用Tkinter進行GUI程式設計

        關于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上應該可以找的到

Python下用Tkinter進行GUI程式設計 Python下用Tkinter進行GUI程式設計

【轉載】:http://www.cnblogs.com/fuyunbiyi/archive/2012/06/13/2548497.html