Python GUI----Button

Source: Internet
Author: User

1. A simple button application
From tkinter Import * #定义Button的回调函数def Hellobutton ():    print (' Hello button ') root = Tk () # Use the Command property to specify the button's callback function button (Root,text = ' Hello Button ', command = Hellobutton). Pack () Root.mainloop ()
2. Test button's Relief property run the following code to see the various effects of the button.
From tkinter Import *root = Tk () #flat, Groove, raised, ridge, solid, or sunkenbutton (root,text = ' Hello button ', Relief=fla T). 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. Like a label, a button can also display text and images at the same time, using attributes compound
From tkinter Import *root = Tk () #图像居下, home, right, left, text on top of 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 () Root.mainloop ()
4. Control focus issues Create three buttons, each corresponding to the callback function, the second button set the focus, the program run is Press "Enter" to determine the program's printing results
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 = ' but Ton2 ') b2.bind ("<Return>", CB2)                            #Return事件相应回车点击. The Enter event responds with MOUSEOVERB3 = Button (root,text = ' Button3 ', command = CB3) b1.pack () B2.pack () B3.pack () B2.focus_set ()                                     # Set the focus on the button B2 Root.mainloop ()
The Bind method is used in the previous example, which establishes the relationship between the event and the callback function (the response function), and whenever the <Enter> event is generated, the program automatically calls CB2, unlike CB1,CB3, which itself has a parameter----event, This parameter passes the information that responds to the event.
5. Specify the width and height of the button: width
Heigth: Height
Set this property in three ways:
1. When creating a button object, specify the width and height
2. Use the property width and height to specify the width and height
3. Use the Configure method to specify width and height

The above three methods are also suitable for other controls

From tkinter Import *root = Tk () B1 = button (Root,text = ' 30x1 ', width = 30,height = 2) b1.pack () b2 = button (Root,text = ' 30X 2 ') b2[' width ' = 30b2[' height '] = 3b2.pack () B3 = Button (root,text = ' 30x3 ') b3.configure (width = 30,height = 3) b3.pack () Roo T.mainloop ()
6. Set the display position of the button text on the control anchor: The values used are: N (North), S (south), W (west), E (East), and NE,NW,SE,SW, which are the identity locations on the map, The width and Height properties are used to show the differences in individual properties.
From Tkinter Import *root = Tk () #简单就是美! For a in [' N ', ' s ', ' e ', ' w ', ' ne ', ' nw ', ' se ', ' SW ']:    Button (root,    text = ' anchor ',    anchor = A,    width = 30,< C4/>height = 4). Pack ()

7. Change the foreground and background color of the button
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 () Root.mainloop ()


8. Set the border of the button BD (bordwidth): Default is 1 or 2 pixels
# Create 5 button border width in order: 0,2,4,6,8from tkinter Import *root = Tk () for b in [0,1,2,3,4]:    Button (root,    text = str (b), 
   BD = b). Pack () Root.mainloop ()    
9. Setting the button state
From tkinter Import *root = Tk () def stateprint ():    print ("state") for R in [' Normal ', ' active ', ' disabled ']:    Button (root,    text = r, state    = r,    width = +,    command = stateprint). Pack () Root.mainloop ()
In the example, set the three button in the callback function to Stateprint, run the program only normal and active activation of the callback function, and the Disable button is not, for the moment do not need the button to work, you can set its state to disabled property

10. Bind button with variable set button in Textvariable property
From tkinter Import *root = Tk () def changetext ():    if b[' text '] = = ' text ':        v.set (' change ')    else:        v.set (' Text ') v = stringvar () b = Button (root,textvariable = V,command = Changetext) v.set (' text ') B.pack () Root.mainloop ()
Binds the variable v to the button, and when the V value changes, the text displayed by the button changes as well








If you have any questions, welcome to my public question ~






Python GUI----Button

Related Article

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.