日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

用python做tkinter_Python下用Tkinter进行GUI编程

發布時間:2024/9/27 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用python做tkinter_Python下用Tkinter进行GUI编程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

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

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

__author__ = 'fyby'

from Tkinter import * #引入Tkinter工具包

defhello():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()#進入主循環,程序運行

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

#-*- encoding=UTF-8 -*-

__author__ = 'fyby'

from Tkinter import *

classApp: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)defsay_hi(self):print "hi there, this is a class example!"win=Tk()

app=App(win)

win.mainloop()

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

#-*- encoding=UTF-8 -*-

__author__ = 'fyby'

from Tkinter import *root=Tk()defhello():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()

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

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

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

#-*- encoding=UTF-8 -*-

__author__ = 'fyby'

from Tkinter import *root=Tk()defhello():print('hello')defabout():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()

寫了這一些,差不多對Tkinter有了一個大體的印象了。在Python中用Tkinter繪制GUI界面還是蠻簡單的。再把上面的例子擴展一下,和Label標簽結合,當單擊about的時候,在窗體上打印About的內容,而不是在終端輸出。將about函數稍微修改一下。單擊about以后將會調用about函數渲染frame繪制一個標簽并顯示其內容。

defabout():

w= Label(root,text="開發者感謝名單\nfuyunbiyi\nfyby尚未出現的女朋友\nhttp://www.programup.com網站")

w.pack(side=TOP)

總結

以上是生活随笔為你收集整理的用python做tkinter_Python下用Tkinter进行GUI编程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。