Tkinter Tutorial: Button (1)

Source: Internet
Author: User
Tags tkinter tutorial
# Button in Tkinter tutorial (1)
# Button trigger event
'''1. A simple Button application '''
From Tkinter import *
# Define the callback function of the Button
Def helloButton ():
Print 'Hello click'
Root = Tk ()
# Specify the callback function of the Button through the command attribute
Button (root, text = 'Hello click', command = helloButton). pack ()
Root. mainloop ()

'''
Execution Result: After you click the button, the program prints the 'hello Button 'to the standard output. The preceding figure shows how to use the button.
To simplify the process, such as not setting the callback function of the Button, this is acceptable, but the result is not too similar to the Label.
The big difference is that the appearance looks different and the function of the Button is lost.
From Tkinter import *
Root = Tk ()
# The following relief = FLAT setting is a Label !!!
Button (root, text = 'Hello click', relief = FLAT). pack ()
Root. mainloop ()
'''
'''2. Test the relief attribute of the Button '''
# Run the following code to view the different effects of the Button. No callback function is available.
From Tkinter import *
Root = Tk ()
# Flat, groove, raised, ridge, solid, or sunken
Button (root, text = 'Hello click', relief = FLAT). pack ()
Button (root, text = 'Hello click', relief = GROOVE). pack ()
Button (root, text = 'Hello click', relief = RAISED). pack ()
Button (root, text = 'Hello click', relief = RIDGE). pack ()
Button (root, text = 'Hello click', relief = SOLID). pack ()
Button (root, text = 'Hello click', relief = SUNKEN). pack ()

Root. mainloop ()

'''
Button display image
Image: You can use gif images. The image loading method is img = PhotoImage (root, file = filepath
Bitmap: the bitmap in X11 format cannot be displayed in Windows. In Windows, use GIMP2.4
The Bitmap generated by the X11 bitmap editor in linux cannot be used after bitmap is converted to an xbm file.
To use the built-in bitmap.
(1). Use bitmap files
Bp = BitmapImage (file = "c:/python2.xbm ")
Button (root, bitmap = bp). pack ()
(2). Use bitmap data
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 tuple data to create images
Bmp = BitmapImage (data = BITMAP)
Button (root, bitmap = bmp)
'''
'''3. Like the Label, the Button can also display text and images at the same time, using the attribute compound '''
From Tkinter import *
Root = Tk ()
# Image bottom, top, right, left, text on the Image
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 ()
# Message loop
Root. mainloop ()

'''4. Control focus
Create three buttons and corresponding callback functions. Set the focus of the second Button and press "Enter" to run the program.
Program printing result
'''
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 ()
'''
In the preceding example, the bind method is used to establish the relationship between the event and the callback function (response function ).
The program automatically calls cb2. Unlike cb1 and cb3, the program also has a parameter ---- event, which is passed
Event Response Information.
'''
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 ()

'''
An error was made and <Return> was written as <Enter>. The result is: when the mouse enters the Button area, the event printEventInfo
Called. The program prints the event information.
'''

# Author: jcodeer
# Blog: jcodeer.cublog.cn
# Email: jcodeer@126.com

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.