Tkinter教程之Button篇(1)

來源:互聯網
上載者:User
#Tkinter教程之Button篇(1)
#Button功能觸發事件
'''1.一個簡單的Button應用'''
from Tkinter import *
#定義Button的回呼函數
def helloButton():
    print 'hello button'
root = Tk()
#通過command屬性來指定Button的回呼函數
Button(root,text = 'Hello Button',command = helloButton).pack()
root.mainloop()

'''
執行的結果:每次點擊一次,程式向標準輸出列印'hello button',以上為Button使用方法,可以
再做一下簡化,如不設定Button的回呼函數,這樣也是允許的但這樣的結果與Label沒有什麼太
大的區別,只是外觀看起來有所不同罷了,失去了Button的作用。
from Tkinter import *
root = Tk()
#下面的relief = FLAT設定,就是一個Label了!!!
Button(root,text = 'hello button',relief=FLAT).pack()
root.mainloop()
'''
'''2.測試Button的relief屬性'''
#運行下面的代碼可以看到Button的各個不同效果,均沒有回呼函數。
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轉換為xbm檔案,依舊無法使用.linux下的X11 bitmap編輯器產生的bitmap還沒有測試,但可
以使用內建的位元影像。
(1).使用位元影像檔案
bp = BitmapImage(file = "c:/python2.xbm")
Button(root,bitmap = bp).pack()
(2).使用位元影像資料
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資料來建立映像
bmp = BitmapImage(data = BITMAP)
Button(root,bitmap = bmp)
'''
'''3.與Label一樣,Button也可以同時顯示文本與映像,使用屬性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()
#訊息迴圈
root.mainloop()

'''4.控制項焦點問題
建立三個Button,各自對應回呼函數;將第二個Button設定焦點,程式運行是按“Enter”,判斷
程式的列印結果
'''
from Tkinter import *

def cb1():
    print 'button1 clicked'
def cb2(event):
    print 'button2 clicked'
def cb3():
    print 'button3 clicked'
    
root = Tk()

b1 = Button(root,text = 'Button1',command = cb1)
b2 = Button(root,text = 'Button2')
b2.bind("<Return>",cb2)
b3 = Button(root,text = 'Button3',command = cb3)
b1.pack()
b2.pack()
b3.pack()

b2.focus_set()
root.mainloop()
'''
上例中使用了bind方法,它建立事件與回呼函數(響應函數)之間的關係,每當產生<Enter>事件
後,程式便自動的調用cb2,與cb1,cb3不同的是,它本身還帶有一個參數----event,這個參數傳遞
響應事件的資訊。
'''
from Tkinter import *
def printEventInfo(event):
    print 'event.time = ' , event.time
    print 'event.type = ' , event.type
    print 'event.WidgetId = ', event.widget
    print 'event.KeySymbol = ',event.keysym
root = Tk()
b = Button(root,text = 'Infomation')
b.bind("<Return>",printEventInfo)
b.pack()
b.focus_set()
root.mainloop()    

'''
犯了個錯誤,將<Return>寫成<Enter>了,結果是:當滑鼠進入Button地區後,事件printEventInfo
被調用。程式列印出了event的資訊。
'''

#author:     jcodeer
#blog:    jcodeer.cublog.cn
#email:    jcodeer@126.com

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.