This is the 23rd day of my participation in the August More Text Challenge.More challenges in August

Review and

We have studied the Tkinter component Label Label. We have studied the commonly used properties of Label and the meaning of the Options option for setting its properties in detail.

We all know that GUI programming-related components inherit from Widget’s parent class. The component diagram looks like this:

In this installment, we will learn about the use of properties and methods associated with a Button Button, a color-coded component.

1. The Button Button

โŒ› migrant workers ordinary day new era, the alarm goes off in the morning, get up shut the alarm switch (1), fingerprint (2) open the IPhone view information, hastily wash good, go out to ride the elevator and pressed on the ground floor (3), catch up with the bus arrived at the company, open the computer power supply (4), jumping on the keyboard (5) at your fingertip, open a busy social animals part-time work…

In the above small life scene, the Button marked with numbers interacts with us and plays an indispensable role in our daily life

๐Ÿ‘‰ what is a Button?

Button A Tkinter standard component used to perform a user’s stand-alone operation and interact with the user.

๐Ÿ‘‰ Main syntax:

W = Button(master,options)
Copy the code
  • Master: the parent container
  • Options: Options are available. Sets key properties

๐Ÿ‘‰ Button features:

  • Can contain text, images
  • When a button is clicked, the corresponding event-bound method is automatically called

2. Compare Button and Label

Button and Label are components of the Python Tkinter standard.

  • The same

    1. Label and Button can both display images and text
    2. Both Label and Button instance objects need pack() to be displayed
    3. Label and Button display image formats only support GIF format
  • The mark

    1. A Label can specify a font, and a Button can use only a single font
    2. A Label can only display static images and text, and a Button can be used in combination to achieve dynamic effects
    3. A Button can bind events to interact with the user

3. Button option

options role
state Button status options, including DISABLED, NORMAL, and ACTIVE
activebackground The background color of the button when the mouse is over it
activeforeground The foreground color of the button when the mouse is over it
bd Size of button border. Default is 2 pixels
bg The background color of the button
fg The foreground color of the button (the color of the button text)
font Text font, text size, text glyphs. Glyph overstrike/italic/bold/underline
height Height of the button, if not set, size to fit the content of the button (text or image size)
width The width of the button, if not set, to fit the content of the button (text or image size)
image The image to be displayed on the button. The image must be assigned to image as a variable. The image must be in GIF format.
justify When displaying multiple lines of text, set the alignment between different lines. Options include LEFT, RIGHT, and CENTER
padx The padding on the X-axis is the distance between the content of the button and the edge of the button
pady The padding of the button in the y direction
relief Border style, set the display effect of controls. Options: FLAT, SUNKEN, RAISED, GROOVE, RIDGE.
wraplength Limit the number of characters that can be displayed on each line of a button
underline The underline. None of the text on the default button is underlined. The value is the underlined string index, 0, the first character is underlined, 1, the second character is underlined, and so on
text The text content of the button

3. Button binding event mode

Method 1: After defining the event, add the Command attribute to the Button object

Def addList(): pass # 2. Button object add command but = Button(root,command=addList)Copy the code

Method 2: Use bind

b1 = Button(root, text="Add")

b1.bind("<Button-1>",addList)

Copy the code

4. Use of Button components

๐Ÿ“ Example: Button duplicates the Label icon

The following code

from tkinter import * class Application(Frame): def __init__(self,master=None): # super() represents the definition of the parent class, Instead of superclass object super().__init__(master) self.master = master self.pack() # self.createWidget() self.createWidget() def createwidget(self): self.but1 = Button(self) self.but1["text"] = "add" self.but1.pack() self.but1["command"] = self.addinfo def Def createWidget(self): global photo photo = PhotoImage(file="heart.gif") self.but = Button(self,image = photo,command = self.addimage) self.but.pack() def addimage(self): Self.labimage = Label(self,image=photo) self.labimage.pack() messagebox.showinfo(" "," "added successfully ") root = Tk() root.geometry("400x300+200+300") root.title("MyfirstAPP") app = Application(master=root) root.mainloop()Copy the code

conclusion

In this installment, we will learn about properties and methods associated with the Button of the Tkinter component.

Button and Label are common and indispensable components. Through our learning and continuous practice, they can be better displayed to users.

The above is the content of this issue, welcome big guys to point up comments and corrections, see you next time ~แƒฆ(ยด แด— ยท ‘) than heart ๐ŸŒน๐ŸŒน แด—