关于tkinter的button的几种用法

参考博客:

http://blog.csdn.net/jcodeer/article/details/1811298

from tkinter import *

root = Tk()

'''
1、了解Button的relief

Button(root,text="hello,button",relief=FLAT).pack()
#relief=FLAT 就像是一个Label
root.mainloop()
'''


'''
2、了解Button的relief的其他形式

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()
'''


#3、Button显示图像(失败)
'''
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).pack()

'''



'''
4、Button中图像与文本的位置(共同显示),使用compound属性
Button(root,text="button",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()
'''

'''
5、控件焦点问题,实际效果是按下enter键自动执行焦点控件,bind方法

def cb1():
    print("button1 clicked")
def cb2(event):
    print("button2 clicked")
def cb3():
    print("button3 clicked")

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()
'''

'''
6、依旧是控件焦点,按下enter后,输出一系列信息,event函数的信息

def printEventInfo(event):
    print('event.time = ',event.time)
    print('event.type = ',event.type)
    print('event.Widgetld = ',event.widget)
    print('event.KeySymbol = ',event.keysym)

but = Button(root,text = "Infomation")
but.bind("<Return>",printEventInfo)
but.pack()
but.focus_set()
'''
#若将Return换成Enter,鼠标移至button处,就会printEventInfo,打印出信息


root.mainloop()


后续参考:

http://blog.csdn.net/aa1049372051/article/details/51859476

#http://blog.csdn.net/aa1049372051/article/details/51859476

'''
7、一共三种方式指定button的宽和高
    一、button内设置width和height属性
    二、使用属性来指定
    三、使用configure方法来指定
b1 = Button(root,text="but1",width=30,height=2)
b1.pack()

b2 = Button(root,text="but2")
b2['width'] = 30
b2['height'] = 2
b2.pack()

b3 = Button(root,text="but3")
b3.configure(width=30,height=2)
b3.pack()
'''




'''
8、anchor指button的文本所在控件的位置,nsew
for a in ['n','s','e','w','ne','nw','se','sw']:
    Button(root,
           text = "anchor",
           anchor = a,
           width = 30,
           height = 3).pack()
以上为简化代码√
'''




'''
9、改变前景色和背景色
bfg = Button(root,text="change boreground",fg = "red")
bfg.pack()
bbg = Button(root,text="change background",bg = "blue")
bbg.pack()
'''




'''
10、bd=bordwidth指边框宽度,缺省值为1或2个像素
for b in [0,1,2,3,4]:
    Button(root,
           text=str(b),
           bd=b).pack()
'''



'''
11、button风格,relief同上2
'''



'''
12、button的状态,三种
def statePrint():
    print("state")
for r in ['normal','active','disabled']:
    Button(root,
           text = r,
           state = r,
           width = 30,
           command = statePrint).pack()
'''



'''
13、绑定button与变量,变量变化,button变化;button变化,变量变化
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()
'''




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值