文章轉(zhuǎn)載自:http://blog.csdn.net/jcodeer/article/details/1811298
- # Tkinter教程之Button篇(1)
- # Button功能觸發(fā)事件
- '''''1.一個(gè)簡單的Button應(yīng)用'''
- from tkinter import *
-
-
- # 定義Button的回調(diào)函數(shù)
- def helloButton():
- print('hello button')
-
-
- root = Tk()
- # 通過command屬性來指定Button的回調(diào)函數(shù)
- Button(root, text='Hello Button', command=helloButton).pack()
- root.mainloop()
-
- '''''
- 執(zhí)行的結(jié)果:每次點(diǎn)擊一次,程序向標(biāo)準(zhǔn)輸出打印'hello button',以上為Button使用方法,可以
- 再做一下簡化,如不設(shè)置Button的回調(diào)函數(shù),這樣也是允許的但這樣的結(jié)果與Label沒有什么太
- 大的區(qū)別,只是外觀看起來有所不同罷了,失去了Button的作用。
- from Tkinter import *
- root = Tk()
- #下面的relief = FLAT設(shè)置,就是一個(gè)Label了?。?!
- Button(root,text = 'hello button',relief=FLAT).pack()
- root.mainloop()
- '''
- '''''2.測試Button的relief屬性'''
- # 運(yùn)行下面的代碼可以看到Button的各個(gè)不同效果,均沒有回調(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='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 = filepath
- bitmap:使用X11 格式的bitmap,Windows的Bitmap沒法顯示的,在Windows下使用GIMP2.4將windows
- Bitmap轉(zhuǎn)換為xbm文件,依舊無法使用.linux下的X11 bitmap編輯器生成的bitmap還沒有測試,但可
- 以使用內(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 32
- static char im_bits[] = {
- 0xaf,0x6d,0xeb,0xd6,0x55,0xdb,0xb6,0x2f,
- 0xaf,0xaa,0x6a,0x6d,0x55,0x7b,0xd7,0x1b,
- 0xad,0xd6,0xb5,0xae,0xad,0x55,0x6f,0x05,
- 0xad,0xba,0xab,0xd6,0xaa,0xd5,0x5f,0x93,
- 0xad,0x76,0x7d,0x67,0x5a,0xd5,0xd7,0xa3,
- 0xad,0xbd,0xfe,0xea,0x5a,0xab,0x69,0xb3,
- 0xad,0x55,0xde,0xd8,0x2e,0x2b,0xb5,0x6a,
- 0x69,0x4b,0x3f,0xb4,0x9e,0x92,0xb5,0xed,
- 0xd5,0xca,0x9c,0xb4,0x5a,0xa1,0x2a,0x6d,
- 0xad,0x6c,0x5f,0xda,0x2c,0x91,0xbb,0xf6,
- 0xad,0xaa,0x96,0xaa,0x5a,0xca,0x9d,0xfe,
- 0x2c,0xa5,0x2a,0xd3,0x9a,0x8a,0x4f,0xfd,
- 0x2c,0x25,0x4a,0x6b,0x4d,0x45,0x9f,0xba,
- 0x1a,0xaa,0x7a,0xb5,0xaa,0x44,0x6b,0x5b,
- 0x1a,0x55,0xfd,0x5e,0x4e,0xa2,0x6b,0x59,
- 0x9a,0xa4,0xde,0x4a,0x4a,0xd2,0xf5,0xaa
- };
- """
- 使用tuple數(shù)據(jù)來創(chuàng)建圖像
- bmp = BitmapImage(data = BITMAP)
- Button(root,bitmap = bmp)
- '''
- '''''3.與Label一樣,Button也可以同時(shí)顯示文本與圖像,使用屬性compound'''
- from tkinter import *
-
- root = Tk()
- # 圖像居下,居上,居右,居左,文字位于圖像之上
- 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, text='center', compound='center', bitmap='error').pack()
- # 消息循環(huán)
- root.mainloop()
- '''''4.控件焦點(diǎ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():
- print('button1 clicked')
-
-
- def printEventInfo(event):
- print('event.time = ', event.time)
- print('event.type = ', event.type)
- print('event.WidgetId = ', event.widget)
- print('event.KeySymbol = ', event.keysym)
-
-
- def cb3():
- print('button3 clicked')
-
-
- root = Tk()
-
- b1 = Button(root, text='Button1', command=cb1)
- b2 = Button(root, text='Button2')
- b2.bind("<Enter>", printEventInfo)
- 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)生<Enter>事件
- 后,程序便自動(dòng)的調(diào)用cb2,與cb1,cb3不同的是,它本身還帶有一個(gè)參數(shù)----event,這個(gè)參數(shù)傳遞
- 響應(yīng)事件的信息。
- '''
- '''''5.指定Button的寬度與高度
- width: 寬度
- heigth: 高度
- 使用三種方式:
- 1.創(chuàng)建Button對(duì)象時(shí),指定寬度與高度
- 2.使用屬性width和height來指定寬度與高度
- 3.使用configure方法來指定寬度與高度
- '''
- from tkinter import *
-
- root = Tk()
- b1 = Button(root, text='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),e(east)和ne,nw,se,sw,就是地圖上的標(biāo)識(shí)位置了,使用
- width和height屬性是為了顯示各個(gè)屬性的不同。
- '''
- from tkinter import *
-
- root = Tk()
-
- # 簡單就是美!
- 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).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,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 = '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='change backgroud', bg='blue')
- bbg.pack()
-
- '''''8.設(shè)置Button的邊框
- bd(bordwidth):缺省為1或2個(gè)像素
- '''
- # 創(chuàng)建5個(gè)Button邊框?qū)挾纫来螢椋?,2,4,6,8
- for b in [0, 1, 2, 3, 4]:
- Button(root,
- text=str(b),
- bd=b).pack()
- '''''9.設(shè)置Button的風(fēng)格
- relief/raised/sunken/groove/ridge
- '''
- for r in ['raised','sunken','groove','ridge']:
- Button(root,
- text = r,
- relief = r,
- width = 30).pack()
-
-
- '''''10.設(shè)置Button狀態(tài)
- normal/active/disabled
- '''
- def statePrint():
- print('state')
- for r in ['normal','active','disabled']:
- Button(root,
- text = r,
- state = r,
- width = 30,
- command = statePrint).pack()
-
- #例子中將三個(gè)Button在回調(diào)函數(shù)都設(shè)置為statePrint,運(yùn)行程序只有normal和active激活了回調(diào)函數(shù),而disable按鈕則沒有,對(duì)于暫時(shí)不
- #需要按鈕起作用時(shí),可以將它的state設(shè)置為disabled屬性
- root.mainloop()
- '''''11.綁定Button與變量
- 設(shè)置Button在textvariable屬性
- '''
- from tkinter import *
-
- root = Tk()
-
-
- def changeText():
- if b['text'] == 'text':
- v.set('change')
- print('change')
- else:
- v.set('text')
- print('text')
-
-
- v = StringVar()
- b = Button(root, textvariable=v, command=changeText)
- v.set('text')
- b.pack()
- root.mainloop()
-
- '''''
- 將變量v與Button綁定,當(dāng)v值變化時(shí),Button顯示的文本也隨之變化
- '''
|