Tkinter完整版_第1頁(yè)
Tkinter完整版_第2頁(yè)
Tkinter完整版_第3頁(yè)
Tkinter完整版_第4頁(yè)
Tkinter完整版_第5頁(yè)
已閱讀5頁(yè),還剩45頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、tkinter 教程這系列教程完全以代碼的形式來(lái)寫(xiě)的,目標(biāo)是: 讀者看代碼和注釋就可以理解代碼的意思。但這里的讀者需要具備的幾項(xiàng)技能:熟悉python 語(yǔ)言的基礎(chǔ),如果還沒(méi)有,先看一下python 的教程吧,英文官方(/tut/tut.html ) ;對(duì)界面編程有一定的了解,知道基本的概念就可以了;對(duì) tk 有興趣,別以為她是已經(jīng)過(guò)時(shí)的技術(shù),如果喪失了學(xué)習(xí)的興趣,那肯定無(wú)法完成了;不要以 ctrl+c/ctrl+v的方式使用本教程(雖然它可以這樣直接運(yùn)行),自己輸入,你會(huì)發(fā)現(xiàn)自己原來(lái)也會(huì)犯這樣的錯(cuò)誤;安裝了python2.5且確認(rèn)安裝了tkinter

2、模塊(默認(rèn)就安裝了,如果你沒(méi)有強(qiáng)制的把它去掉的話(huà)) ,下載 python2.5 (/download/) ;如果在閱讀教程中有不明白的,不要強(qiáng)迫自己,直接跳過(guò)去,繼續(xù)下一個(gè)內(nèi)容。tkinter 教程系列教程的特點(diǎn):他不是一本經(jīng)過(guò)文字潤(rùn)色的文章,全部是代碼, 作者在必要的時(shí)候使用注釋來(lái)解釋?zhuān)灰越M件為章節(jié)進(jìn)行介紹,每個(gè)組件又分為不同的例子,各個(gè)例子可以單獨(dú)使用,分別使用序號(hào)標(biāo)注;各個(gè)例子的使用“ 注釋 +序號(hào) ” 的格式表示開(kāi)始, 下一個(gè)例子的開(kāi)始為上一個(gè)例子的結(jié)束;全部使用結(jié)構(gòu)化編程(sp) ,沒(méi)有面向?qū)ο蟮母拍?oo);基本上包含了tkinter的所有的

3、控件,根據(jù)每個(gè)控件的使用方法,選擇性的介紹了其 屬 性 和 方 法 , 沒(méi) 有 全 部 介 紹 , 全 部 的 介 紹 查 看tkinter的 官 方 參 考(http:/ 參考的描述完成,原因由于作者沒(méi)有看懂: (參考書(shū)籍: http:/ 如有沖突以tkinter參考為準(zhǔn)最后祝各位tk 一路快樂(lè)!label#tkinter教程之 label 篇1.label的第一個(gè)例子text屬性使用方法#要使用 tk 模塊,除非你不想使用這個(gè)模塊,那整個(gè)教程就不需要看了from tkinter import*#初始化 tk root = tk ()#創(chuàng)建一個(gè)label ,使用編碼, 到現(xiàn)在為止還沒(méi)有使用過(guò)

4、直接通過(guò)“drag- and- drop”就可以完成的 ide。label = label ( root , text = hello tkinter)#顯示 label ,必須含有此語(yǔ)句label .pack()#root .pack()#但 root 是不需要(嚴(yán)格地說(shuō)是必須不這樣使用),否則解釋器抱怨#進(jìn)入消息循環(huán)root . mainloop ()#控件的顯示步驟:#1. 創(chuàng)建這個(gè)控件#2. 指定這個(gè)空間的master ,即這個(gè)控件屬于哪一個(gè)#3. 告訴 gm ( geometry manager ) 有一個(gè)控件產(chǎn)生了還有更簡(jiǎn)單的一個(gè)例子:將hello tkinter打印到標(biāo)題上,la

5、bel 也不用創(chuàng)建了from tkinter import *root = tk()root.title(hello tkinter)root.mainloop()再?zèng)]法兒簡(jiǎn)化了,就這樣吧2.在 label上使用內(nèi)置位圖bitmap 的使用方法from tkinter import*#初始化 tk root = tk ()#創(chuàng)建一個(gè)label ,使用編碼, 到現(xiàn)在為止還沒(méi)有使用過(guò)直接通過(guò)“drag- and- dro p”就可以完成的 ide。label = label ( root , bitmap = error)#上面的代碼使用了內(nèi)置位圖error #顯示 label ,必須含有此語(yǔ)句l

6、abel .pack()#進(jìn)入消息循環(huán)root . mainloop ()其他可用的位圖: * error * hourglass * info * questhead * question * warning * gray12 * gray25 * gray50 * gray75若要查看各自的效果,可以使用相應(yīng)的名稱(chēng)將bitmpa = error 替換。據(jù)說(shuō)還可以使用自己指定的位圖文件, 網(wǎng)上找了一下,格式如下: label(root, bitmap=/path/bitmapname)不過(guò)我試了一下,從來(lái)沒(méi)有成功過(guò),我已經(jīng)將位圖該為單色的了:(另:還有的網(wǎng)上的文章說(shuō)明如何使用photoima

7、ge 和 bitmapimage 顯示 bmp或 gif文件,提到一點(diǎn)防止圖像文件被python 自動(dòng)回收 (garbage collected), 應(yīng)將 bmp或 gif放到全局 (global)或?qū)嶓w(instance)中,使用如下兩種方法,仍未奏效:#使用 image 屬性# bm = photoimage ( file = c:python.gif)# label = label ( root , image = bm)# label. bm = bm #錯(cuò)誤信息:#tclerror: image pyimagexx doesn t exist#使用 bitmap 屬性# bm = b

8、itmapimage(file=cpython2 . bmp )# label = label(root,bitmap=bm)# label.bm = bm# label.pack()#錯(cuò)誤信息:#tclerror: format error in bitmap data雖然二者均沒(méi)有起作用,還是要說(shuō)明一下,bitmap 與 image 的關(guān)系,如果同時(shí)指定這兩參數(shù), image 優(yōu)先。3. 改變控件的前景色和背景色fg : 前景色bg: 背景色設(shè)置背景色的一個(gè)大的用處是:可以判斷控件的大?。ú煌目丶褂貌煌念伾?,后續(xù)內(nèi)容可以使用此特性來(lái)調(diào)試container)from tkinter i

9、mport *root = tk()#在創(chuàng)建 label 時(shí)指定各自使用的顏色可以使用的顏色值:#使用顏色名稱(chēng)label(root,fg = red ,bg = blue ,text = hello i am tkinter).pack()#使用顏色值 #rrggbblabel(root,fg = red ,bg = #ff00ff,text = hello i am tkinter).pack()#使用系統(tǒng)相關(guān)的顏色值(windows) ,不建議使用這樣的值,不利于平臺(tái)移植label(root,fg = red ,bg = systembuttonshadow ,text = hello i

10、 am tkinter).pack()root.mainloop()( 1). 使用顏色名稱(chēng)red green blue yellow lightblue .( 2). 使用 #rrggbb label = label ( root , fg = red, bg = #ff00ff , text = hello i am tkinter)指定背景色為緋紅色( 3). 除此之外, tk 還支持與os相關(guān)的顏色值,如windows支持systemactiveborder,systemactivecaption,systemappworkspace,systembackground ,.4. 設(shè)置寬

11、度與高度width :寬度height :高度f(wàn)rom tkinter import *root = tk()#創(chuàng)建三個(gè)label ,分別顯示red,blue,yellow#注意三個(gè)label 的大小,它們均與文本的長(zhǎng)度有關(guān)label(root,text = red,bg = red ).pack()label(root,text = blue ,bg = blue ).pack()label(root,text = yellow ,bg = yellow ).pack()#再創(chuàng)建三個(gè)label ,與上次不同的是這三個(gè)label 均使用 width 和 heigth屬性#三個(gè) label 的大

12、小由width 和 height指定label(root,bg = red ,width = 10,height = 3).pack()label(root,bg = blue ,width = 10,height = 3).pack()label(root,bg = yellow ,width = 10,height = 3).pack()root.mainloop()5. 同時(shí)使用圖像與文本compound :指定文本 (text ) 與圖像 ( bitmap / image) 是如何在 label 上顯示,缺省為 none,當(dāng)指定 image/ bitmap 時(shí),文本 ( text )

13、將被覆蓋,只顯示圖像了??梢允褂玫闹担?left:圖像居左 right:圖像居右 top:圖像居上 bottom:圖像居下 center:文字覆蓋在圖像上bitmap / image:顯示在 label 上的圖像text :顯示在 label 上的文本label = label ( root , text = error, compound = left, bitmap = error)from tkinter import *root = tk()#演示 compound的使用方法#圖像與文本在label 中的位置#圖像居下label(root,text = botton ,compound

14、 = bottom ,bitmap = error ).pack()#圖像居上label(root,text = top ,compound = top ,bitmap = error ).pack()#圖像居右label(root,text = right ,compound = right ,bitmap = error ).pack()#圖像居左label(root,text = left,compound = left,bitmap = error ).pack()#文字覆蓋在圖像上label(root,text = center ,compound = center ,bitmap

15、= error ).pack()#消息循環(huán)root.mainloop()6. 文本的多行顯示在 tk004中,使用width 和 heigth來(lái)指定控件的大小,如果指定的大小無(wú)法滿(mǎn)足文本的要求是,會(huì)出現(xiàn)什么現(xiàn)象呢?如下代碼: label( root , bg = welcome to , width = 10, height = 3 ). pack ()運(yùn)行程序,超出label的那部分文本被截?cái)嗔?,常用的方法是:使用自?dòng)換行功能,及當(dāng)文本長(zhǎng)度大于控件的寬度時(shí),文本應(yīng)該換到下一行顯示,tk 不會(huì)自動(dòng)處理,但提供了屬性:wraplength :指定多少單位后開(kāi)始換行justify:指定多行的對(duì)齊方

16、式ahchor :指定文本 ( text ) 或圖像 ( bitmap / image) 在 label 中的顯示位置可用的值:e w n s ne se sw sn center 布局如下圖 nw n ne w center e sw s se from tkinter import *root = tk()#左對(duì)齊,文本居中l(wèi)abel(root,text = welcome to jcodeer. cublog . cn,bg = yellow ,width = 40,height = 3,wraplength = 80,justify = left).pack()#居中對(duì)齊,文本居左la

17、bel(root,text = welcome to jcodeer. cublog . cn,bg = red ,width = 40,height = 3,wraplength = 80,anchor = w).pack()#居中對(duì)齊,文本居右label(root,text = welcome to jcodeer. cublog . cn,bg = blue ,width = 40,height = 3,wraplength = 80,anchor = e).pack()root.mainloop()運(yùn)行一下程序就可以直觀的看出,justify與 anchor 的區(qū)別了: 一個(gè)用于控制多

18、行的對(duì)齊;另一個(gè)用于控制整個(gè)文本塊在label 中的位置button(1)#jtkinter教程之 button 篇( 1)#button 功能觸發(fā)事件1.一個(gè)簡(jiǎn)單的button 應(yīng)用 from tkinter import*#定義 button的回調(diào)函數(shù)def hellobutton():printhello buttonroot = tk ()#通過(guò) command屬性來(lái)指定button 的回調(diào)函數(shù)button ( root , text = hello button, command = hellobutton). pack ()root . mainloop ()執(zhí)行的結(jié)果 : 每次點(diǎn)

19、擊一次, 程序向標(biāo)準(zhǔn)輸出打印 hello button , 以上為 button 使用方法,可以再做一下簡(jiǎn)化,如不設(shè)置button 的回調(diào)函數(shù),這樣也是允許的但這樣的結(jié)果與label 沒(méi)有什么太大的區(qū)別,只是外觀看起來(lái)有所不同罷了,失去了button 的作用。from tkinter import *root = tk()#下面的 relief = flat設(shè)置,就是一個(gè)label 了! ! !button(root,text = hello button,relief=flat).pack()root.mainloop()2.測(cè)試 button 的 relief屬性 #運(yùn)行下面的代碼可以看到

20、button 的各個(gè)不同效果,均沒(méi)有回調(diào)函數(shù)。from tkinter import*root = tk ()#flat, groove , raised, ridge, solid,or sunken button ( root , text = hello button, relief=flat). pack()button ( root , text = hello button, relief=groove). pack()button ( root , text = hello button, relief=raised ). pack()button ( root , text =

21、 hello button, relief=ridge ). pack ()button ( root , text = hello button, relief=solid ). pack ()button ( root , text = hello button, relief=sunken ). pack()root . mainloop ()button 顯示圖像image: 可以使用gif圖像,圖像的加載方法img = photoimage(root,file = filepathbitmap: 使用 x11 格式的 bitmap,windows 的 bitmap 沒(méi)法顯示的, 在

22、windows下使用 gimp2.4將 windowsbitmap 轉(zhuǎn)換為 xbm文件,依舊無(wú)法使用.linux下的 x11 bitmap編輯器生成的bitmap 還沒(méi)有測(cè)試,但可以使用內(nèi)置的位圖。(1). 使用位圖文件bp = bitmapimage(file = c:python2.xbm)button(root,bitmap = bp).pack()(2). 使用位圖數(shù)據(jù)bitmap = #define im_width 32#define im_height 32static char im_bits = 0 xaf,0 x6d,0 xeb,0 xd6,0 x55,0 xdb,0 xb

23、6,0 x2f,0 xaf,0 xaa,0 x6a,0 x6d,0 x55,0 x7b,0 xd7,0 x1b,0 xad,0 xd6,0 xb5,0 xae,0 xad,0 x55,0 x6f,0 x05,0 xad,0 xba,0 xab,0 xd6,0 xaa,0 xd5,0 x5f,0 x93,0 xad,0 x76,0 x7d,0 x67,0 x5a,0 xd5,0 xd7,0 xa3,0 xad,0 xbd,0 xfe,0 xea,0 x5a,0 xab,0 x69,0 xb3,0 xad,0 x55,0 xde,0 xd8,0 x2e,0 x2b,0 xb5,0 x6a,0 x6

24、9,0 x4b,0 x3f,0 xb4,0 x9e,0 x92,0 xb5,0 xed,0 xd5,0 xca,0 x9c,0 xb4,0 x5a,0 xa1,0 x2a,0 x6d,0 xad,0 x6c,0 x5f,0 xda,0 x2c,0 x91,0 xbb,0 xf6,0 xad,0 xaa,0 x96,0 xaa,0 x5a,0 xca,0 x9d,0 xfe,0 x2c,0 xa5,0 x2a,0 xd3,0 x9a,0 x8a,0 x4f,0 xfd,0 x2c,0 x25,0 x4a,0 x6b,0 x4d,0 x45,0 x9f,0 xba,0 x1a,0 xaa,0 x7

25、a,0 xb5,0 xaa,0 x44,0 x6b,0 x5b,0 x1a,0 x55,0 xfd,0 x5e,0 x4e,0 xa2,0 x6b,0 x59,0 x9a,0 xa4,0 xde,0 x4a,0 x4a,0 xd2,0 xf5,0 xaa;使用 tuple數(shù)據(jù)來(lái)創(chuàng)建圖像bmp = bitmapimage(data = bitmap)button(root,bitmap = bmp)3.與 label 一樣, button 也可以同時(shí)顯示文本與圖像,使用屬性compoundfrom tkinter import*root = tk ()#圖像居下 , 居上,居右,居左,文字位于圖

26、像之上button ( root , text = botton, compound = bottom, bitmap = error). pack()button ( root , text = top, compound = top, bitmap = error). pack()button ( root , text = right, compound = right,bitmap = error). pack ()button ( root , text = left, compound = left, bitmap = error). pack()button ( root , t

27、ext = center, compound = center, bitmap = error). pack()#消息循環(huán)root . mainloop ()4.控件焦點(diǎn)問(wèn)題創(chuàng)建三個(gè)button , 各自對(duì)應(yīng)回調(diào)函數(shù); 將第二個(gè)button 設(shè)置焦點(diǎn),程序運(yùn)行是按“ enter ”,判斷程序的打印結(jié)果from tkinter import*def cb1 ():printbutton1 clickeddef cb2 ( event ):printbutton2 clickeddef cb3 ():printbutton3 clickedroot = tk ()b1 = button ( roo

28、t , text = button1, command = cb1 )b2 = button ( root , text = button2)b2. bind ( , cb2)b3 = button ( root , text = button3, command = cb3 )b1. pack()b2. pack()b3. pack()b2. focus_set()root . mainloop ()上例中使用了bind 方法,它建立事件與回調(diào)函數(shù)(響應(yīng)函數(shù)) 之間的關(guān)系, 每當(dāng)產(chǎn)生 事件后,程序便自動(dòng)的調(diào)用cb2,與 cb1,cb3 不同的是,它本身還帶有一個(gè)參數(shù)-event,這個(gè)參數(shù)傳遞

29、響應(yīng)事件的信息。from tkinter import*def printeventinfo( event ):printevent.time = , event . time printevent.type = , event . type printevent.widgetid = , event . widget printevent.keysymbol = , event . keysym root = tk ()b = button ( root , text = infomation)b. bind ( , printeventinfo)b. pack()b. focus_set

30、()root . mainloop ()犯了個(gè)錯(cuò)誤,將 寫(xiě)成 了,結(jié)果是:當(dāng)鼠標(biāo)進(jìn)入button區(qū)域后,事件printeventinfo被調(diào)用。程序打印出了event 的信息。button(2)# tkinter教程之 button 篇( 2)5.指定 button 的寬度與高度width: 寬度heigth: 高度使用三種方式:1. 創(chuàng)建 button對(duì)象時(shí),指定寬度與高度2. 使用屬性width 和 height來(lái)指定寬度與高度3. 使用 configure方法來(lái)指定寬度與高度f(wàn)rom tkinter import*root = tk ()b1 = button ( root , text

31、 = 30x1 , width = 30 , height = 2 )b1. pack()b2 = button ( root , text = 30x2 )b2 width= 30 b2 height= 3 b2. pack()b3 = button ( root , text = 30x3 )b3. configure( width = 30 , height = 3 )b3. pack()root . mainloop ()# 上述的三種方法同樣也適合其他的控件6.設(shè)置 button 文本在控件上的顯示位置anchor :使用的值為 :n(north),s(south),w(west),

32、e(east)和 ne,nw,se,sw , 就是地圖上的標(biāo)識(shí)位置了,使用width 和 height屬性是為了顯示各個(gè)屬性的不同。from tkinter import*root = tk ()#簡(jiǎn)單就是美!for a in n , s , e ,w , ne , nw , se, sw : button( root , text = anchor, anchor = a , width = 30 , height = 4 ). pack()#如果看的不習(xí)慣,就使用下面的代碼。# button ( root , text = anchor, width = 30 , height =4).

33、pack()# button ( root , text = anchor, anchor = center, width = 30 , height =4). pack()# button ( root , text = anchor, anchor = n , width = 30 , height = 4 ). pack()# button ( root , text = anchor, anchor = s, width = 30 , height = 4 ). pack()# button ( root , text = anchor, anchor = e , width = 30

34、 , height = 4 ). pack()# button ( root , text = anchor, anchor = w , width = 30 , height = 4 ). pack()# button ( root , text = anchor, anchor = ne , width = 30 ,height = 4 ). pack()# button ( root , text = anchor, anchor = nw , width = 30 ,height = 4 ). pack()# button ( root , text = anchor, anchor

35、= se, width = 30 ,height = 4 ). pack()# button ( root , text = anchor, anchor = sw , width = 30 ,height = 4 ). pack()root . mainloop ()7.改變 button 的前景色與背景色fg: 前景色bg:背景色from tkinter import*root = tk ()bfg = button ( root , text = change foreground, fg = red)bfg . pack()bbg = button ( root , text = ch

36、ange backgroud, bg = blue)bbg. pack()root . mainloop ()8.設(shè)置 button 的邊框bd(bordwidth):缺省為 1或2個(gè)像素# 創(chuàng)建 5個(gè) button 邊框?qū)挾纫来螢椋?,2, 4,6,8 from tkinter import*root = tk ()for b in 0, 1, 2, 3, 4: button( root , text = string( b), bd = b ). pack()root . mainloop ()9.設(shè)置 button 的風(fēng)格relief/raised/sunken/groove/ridge

37、from tkinter import*root = tk ()for r in raised, sunken, groove, ridge: button( root , text = r , relief = r , width = 30 ). pack()root . mainloop ()10.設(shè)置 button 狀態(tài)normal/active/disabledfrom tkinter import*root = tk ()def stateprint():printstatefor r in normal, active, disabled: button( root , text

38、= r , state = r , width = 30 , command = stateprint). pack()root . mainloop ()#例子中將三個(gè)button 在回調(diào)函數(shù)都設(shè)置為stateprint, 運(yùn)行程序只有normal 和 active激活了回調(diào)函數(shù),而disable按鈕則沒(méi)有,對(duì)于暫時(shí)不#需要按鈕起作用時(shí),可以將它的state設(shè)置為 disabled屬性11.綁定 button 與變量設(shè)置 button 在 textvariable屬性from tkinter import*root = tk ()def changetext ():if b text= te

39、xt: v. set ( change)printchangeelse : v. set ( text)printtextv = stringvar()b = button ( root , textvariable = v , command = changetext )v. set (text)b. pack()root . mainloop ()將變量 v 與 button 綁定,當(dāng)v 值變化時(shí), button 顯示的文本也隨之變化entry #tkinter教程之 entry 篇#entry 用來(lái)輸入單行文本1.第一個(gè) entry 程序 from tkinter import*root

40、 = tk ()entry (root , text = input your text here). pack()root . mainloop ()#上面的代碼目的是創(chuàng)建一個(gè)entry對(duì)象,并在entry上顯示 input your text here, 運(yùn)行此代碼,并沒(méi)有看到文本的顯示,由此可知與lable 和 button 不同, entry的 text屬性不可以設(shè)置entry 的文本2.在 entry 中設(shè)定初始值,使用textvariable將變量與entry 綁定 from tkinter import*root = tk ()e = stringvar()entry = ent

41、ry ( root , textvariable = e )e. set (input your text here)entry .pack()root . mainloop ()#上面的例子中將變量e 與 entry 綁定,然后將e 的值設(shè)置為 input your text here,程序運(yùn)行時(shí)的初始值便設(shè)置了。3.設(shè)置為只讀entry.entry 的另一個(gè)比較有用的屬性,設(shè)置為只讀,不允許用戶(hù)對(duì)它的值改變。設(shè)置 state屬性為 readonly from tkinter import*root = tk ()e = stringvar()entry = entry ( root , t

42、extvariable = e )e. set (input your text here)entry .pack()entry state = readonlyroot . mainloop ()#實(shí)際上 entry 的屬性值可以使用的也為normal / active/ disabled,readonly與 disabled一樣4.設(shè)置為密碼輸入框#將 entry 作為一個(gè)密碼輸入框來(lái)使用,即不顯示用戶(hù)輸入的內(nèi)容值,用特定符號(hào)代替。使用用屬性show來(lái)指定。from tkinter import*root = tk ()e = stringvar()entry = entry ( root

43、 , textvariable = e )e. set (input your text here)entry .pack()#使用 *來(lái)顯示輸入的內(nèi)容,如果喜歡可以改為其它字符entry show = *#分別使用 *#$ 顯示輸入的文本內(nèi)容for mask in *, # , $ : e = stringvar() entry = entry ( root , textvariable = e ) e.set ( password) entry. pack() entry show = mask root . mainloop ()5.驗(yàn)證輸入的內(nèi)容是否符合要求。使用 validate來(lái)校

44、驗(yàn)輸入的內(nèi)容使用 validate方法來(lái)限制輸入的內(nèi)容這是一個(gè)有問(wèn)題的例子,無(wú)法調(diào)用validatetext回調(diào)函數(shù)from tkinter import*root = tk ()e = stringvar()def validatetext( contents ):print contents return contents. isalnum ()entry =entry (root , validate =key, textvariable =e, validatecommand =validatetext)entry .pack()root . mainloop ()文檔中說(shuō)明使用val

45、idate來(lái)接受的事件,使用validatecommand來(lái)確定輸入的內(nèi)容是否合法,但如何傳入?yún)?shù)?沒(méi)找到相應(yīng)的說(shuō)明#還有其他的屬性fg / bg/ relief/width / height / justify/ state使用方法與button相同,不再舉例。checkbutton #tkinter教程之 checkbutton篇#checkbutton又稱(chēng)為多選按鈕,可以表示兩種狀態(tài):on和 off ,可以設(shè)置回調(diào)函數(shù),每當(dāng)點(diǎn)擊此按鈕時(shí)回調(diào)函數(shù)被調(diào)用1.一個(gè)簡(jiǎn)單的checkbutton例子 #創(chuàng)建一個(gè)checkbutton , 顯示文本為 pythonfrom tkinter impor

46、t*root = tk ()checkbutton ( root , text = python). pack ()root . mainloop ()2.設(shè)置 checkbutton的回調(diào)函數(shù) from tkinter import*def callcheckbutton():printyou check this buttonroot = tk ()checkbutton ( root , text = check python,command = callcheckbutton). pack()root . mainloop ()#不管 checkbutton的狀態(tài)如何,此回調(diào)函數(shù)都會(huì)被

47、調(diào)用3.通過(guò)回調(diào)函數(shù)改變checkbutton的顯示文本text的值 from tkinter import*def callcheckbutton(): #改變 v 的值,即改變checkbutton的顯示值 v.set ( check tkinter)root = tk ()v = stringvar()v. set (check python)#綁定 v 到 checkbutton的屬性 textvariable checkbutton ( root , text =check python , textvariable =v,command =callcheckbutton). pac

48、k ()root . mainloop ()4.上述的textvariable使用方法與button的用法完全相同,使用此例是為了區(qū)別checkbutton的另外的一個(gè)屬性variable,此屬性與textvariable不同,它是與這個(gè)控件本身綁定, checkbutton自己有值: on和 off 值,缺省狀態(tài)on為1,off 為0,如: #顯示 checkbutton的值from tkinter import*root = tk ()#將一整數(shù)與checkbutton的值綁定,每次點(diǎn)擊checkbutton ,將打印出當(dāng)前的值v = intvar()def callcheckbutton

49、():print v . get ()checkbutton ( root , variable = v , text = checkbutton value, command = callcheckbutton). pack()root . mainloop ()5.checkbutton的值不僅僅是1或 0,可以是其他類(lèi)型的數(shù)值,可以通過(guò)onvalue和offvalue屬性設(shè)置checkbutton的狀態(tài)值,如下代碼將on設(shè)置為 python ,off值設(shè)置為 tkinter ,程序的打印值將不再是0或1,而是 tkinter 或 python from tkinter import *r

50、oot = tk()#將一字符串與checkbutton的值綁定,每次點(diǎn)擊checkbutton ,將打印出當(dāng)前的值v = stringvar()def callcheckbutton(): print v.get()checkbutton(root, variable = v, text = checkbutton value, onvalue = python , #設(shè)置 on的值 offvalue = tkinterradiobutton#tkinter教程之 radiobutton篇#radiobutton為單選按鈕,即在同一組內(nèi)只能有一個(gè)按鈕被選中,每當(dāng)選中組內(nèi)的一個(gè)按鈕時(shí),其它的按

51、鈕自動(dòng)改為非選中態(tài),與其他控件不同的是:它有組的概念1.創(chuàng)建一個(gè)簡(jiǎn)單的radiobuttonfrom tkinter import*root = tk ()radiobutton( root , text = python). pack ()radiobutton( root , text = tkinter). pack()radiobutton( root , text = widget). pack ()root . mainloop ()#不指定綁定變量,每個(gè)radiobutton自成一組2.創(chuàng)建一個(gè) radiobutton組,使用綁定變量來(lái)設(shè)置選中哦的按鈕from tkinter i

52、mport*root = tk ()#創(chuàng)建一個(gè)radiobutton組,創(chuàng)建三個(gè)radiobutton,并綁定到整型變量v #選中 value =1的按鈕v = intvar()v. set (1)for i in range ( 3): radiobutton( root , variable = v , text = python, value = i ). pack()root . mainloop ()3.創(chuàng)建兩個(gè)不同的組from tkinter import*root = tk ()vlang = intvar()vos = intvar()vlang.set ( 1)vos . s

53、et ( 2)for v in vlang, vos : #創(chuàng)建兩個(gè)組for i in range ( 3): #每個(gè)組含有 3個(gè)按鈕 radiobutton( root , variable = v , value = i , text = python+ str( i ). pack ()root . mainloop ()#不同的組,各個(gè)按鈕互不影響。4.如果同一個(gè)組中的按鈕使用相同的alue ,則這兩個(gè)按鈕的工作方式完全相同# -*- coding : cp936 -*-from tkinter import*root = tk ()v = intvar()v. set (1)for

54、i in range ( 3): radiobutton( root , variable = v , value = 1 , text = python+ str( i ). pack()for i in range ( 3): radiobutton( root , variable = v , value = i , text = python+ str( 2 + i ). pack ()root . mainloop ()#上述的例子中共有4個(gè) alue為1的值,當(dāng)選中其中的一個(gè)時(shí),其他三個(gè)也會(huì)被選中;選中除了這四個(gè)只外的按鈕時(shí),四個(gè)按鈕全部取消5.與 checkbutton類(lèi)似,每個(gè)

55、radiobutton可以有自己的處理函數(shù),每當(dāng)點(diǎn)擊按鈕時(shí),系統(tǒng)會(huì)調(diào)用相應(yīng)的處理函數(shù)# -*- coding : cp936 -*-from tkinter import*root = tk ()v = intvar()v. set (0)def r1 ():printcall r1def r2 ():printcall r2def r3 ():printcall r3def r4 ():printcall r4i = 0 #創(chuàng)建 8個(gè)按鈕,其中兩個(gè)兩個(gè)的value 值相同for r in r1 , r2, r3 , r4 : radiobutton( root , variable = v

56、, text = radio button, value = i , command = r ). pack () radiobutton( root , variable = v , text = radio button, value = i , command = r ). pack () i += 1 root . mainloop ()#注意雖然同時(shí)可以選中兩個(gè)按鈕,但每次點(diǎn)擊按鈕,執(zhí)行的代碼只有一次6.radiobutton另一個(gè)比較實(shí)用的屬性是indicatoron,缺省情況下為1,如果將這個(gè)屬性改為 0,則其外觀是sunkenfrom tkinter import*root =

57、 tk ()v = intvar()v. set (1)for i in range ( 3): radiobutton( root , variable = v , indicatoron = 0 , text = python & tkinter, value = i ). pack ()root . mainloop ()#radiobutton表示按鈕的彈起或按下兩種狀態(tài)listbox #tkinter教程之 listbox篇#listbox為列表框控件,它可以包含一個(gè)或多個(gè)文本項(xiàng)( text item ) ,可以設(shè)置為單選或多選1.創(chuàng)建一個(gè) listbox ,向其中添加三個(gè)i

58、temfrom tkinter import*root = tk ()lb = listbox( root )for item in python,tkinter, widget: lb. insert( end , item )lb . pack()root . mainloop ()2.創(chuàng)建一個(gè)可以多選的listbox,使用屬性selectmaodfrom tkinter import*root = tk ()lb = listbox( root , selectmode = multiple)for item in python,tkinter, widget: lb. insert(

59、end , item )lb . pack()root . mainloop ()# 依次點(diǎn)擊這三個(gè)item ,均顯示為選中狀態(tài)。# 屬性 multiple允許多選,每次點(diǎn)擊item ,它將改變自己的當(dāng)前選狀態(tài),與checkbox 有點(diǎn)相似3.這個(gè)屬性 selectmode 還可以設(shè)置為browse, 可以通過(guò)鼠標(biāo)來(lái)移動(dòng)listbox中的選中位置(不是移動(dòng)item ) ,這個(gè)屬性也是listbox在默認(rèn)設(shè)置的值,這個(gè)程序與1. 程序運(yùn)行的結(jié)果的一樣的。from tkinter import*root = tk ()lb = listbox( root , selectmode = browse

60、 )for item in python,tkinter, widget: lb. insert( end , item )lb . pack()root . mainloop ()#使用鼠標(biāo)進(jìn)行拖動(dòng),可以看到選中的位置隨之變化。# 與 browse 相似的為 single ,但不支持鼠標(biāo)移動(dòng)選中位置。from tkinter import*root = tk ()lb = listbox( root , selectmode = browse )for item in python,tkinter, widget: lb. insert( end , item )lb . pack()root .

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論