Several uses of buttons in tkinter


 Reference blog: 
 

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

from tkinter import *

root = Tk()

'''
1. Understand the relief of Button

Button(root,text="hello,button",relief=FLAT).pack()
#relief=FLAT is like a Label
root.mainloop ()
'''


'''
2. Understand other forms of Button's 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 display image (failure)
'''
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
};
"""
#Use the tuple data to create the image
bmp = BitmapImage(data = BITMAP)
Button(root,bitmap = bmp).pack()

'''



'''
4. The position of the image and text in the Button (displayed together), using the compound attribute
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. The control focus problem, the actual effect is to press the enter key to automatically execute the focus control, bind method

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. It is still the focus of the control. After pressing enter, a series of information is output, the information of the event function

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()
'''
#If you replace Return with Enter, move the mouse to the button, it will printEventInfo and print out the information


root.mainloop ()


Follow-up reference:

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

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

'''
7. There are three ways to specify the width and height of the button
    1. Set the width and height properties in the button
    2. Use attributes to specify
    Third, use the configure method to specify
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 refers to the position of the control where the text of the button is located, nsew
for a in ['n', 's', 'e', ​​'w', 'ne', 'nw', 'se', 'sw']:
    Button(root,
           text = "anchor",
           anchor = a,
           width = 30,
           height = 3).pack()
The above is simplified code √
'''




'''
9. Change the foreground and background colors
bfg = Button(root,text="change boreground",fg = "red")
bfg.pack()
bbg = Button(root,text="change background",bg = "blue")
bbg.pack ()
'''




'''
10. bd=bordwidth refers to the width of the border, the default value is 1 or 2 pixels
for b in [0,1,2,3,4]:
    Button(root,
           text=str(b),
           bd=b).pack()
'''



'''
11. Button style, relief is the same as above 2
'''



'''
12. The state of the button, three
def statePrint():
    print("state")
for r in ['normal','active','disabled']:
    Button(root,
           text = r,
           state = r,
           width = 30,
           command = statePrint).pack()
'''



'''
13. Bind button and variable, variable change, button change; button change, variable change
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()
'''




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325500708&siteId=291194637