This is the 26th day of my participation in the August Text Challenge.More challenges in August

Review and

Didi, while we’re walking around the neighborhood, we occasionally receive a questionnaire from the nuggets assistant. Let’s take a look at the components of the questionnaire

  • Each column description and chart is a Label component
  • The name Entry field is a single line Entry field
  • Gender selection and degree selection are Radiobutton single box components
  • Year of birth is a drop down box.
  • The other boxes are Text multiple Text input boxes
  • Finally, after filling in the content, click submit Button is Button component

The following components are standard components of Python Tkinter. In this installment, we will learn about the properties and methods of Radiobutton singletons. Let’s go~

1. Overview of Radiobutton checkboxes

Python Tkinter provides Radiobutton for radio selection, which is generally suitable for scenarios that require a single option such as gender, education, etc

Radiobutton single box features

  • Text and images can be added in a single box
  • If this option is selected, the specified method can be executed
  • Radio check provides a get method to get the checked value
  • Each set of Radiobutton components should be associated with only one variable, and each button should represent a single value for that variable

Radiobutton syntax format

Ra = Radiobutton(master,options)
Copy the code

2. Common properties of Radiobutton single box

The property name meaning
text Text display in a single box
variable The function that is executed by the associated checkbox
value Used to distinguish multiple checkbox values
set(value) The specified checkbox is selected by default
relief , according to single box border style options include FLAT/SUNKEN/RAISED/GROOVE RIDGE
height The height of the singleton box needs to be combined with the border style of the singleton box to show the effect
width The width of the singleton box needs to be combined with the border style of the singleton box to show the effect
bd The width of the singleton border style, which needs to be combined with the singleton border style to show the effect
activebackground The foreground color that appears when the mouse clicks on a menu box
activeforeground The background color that appears when the mouse clicks on the menu box
bg The foreground color shown in the checkbox
fg The background color displayed in a single box
font Single box text font, font size, font, font options including bold or italic/underline/overstrike
image A single box displays the image, which must be in GIF format, and which needs to be assigned to the variable by PhotoImage, which then assigns to image
justify Text alignment options include LEFT, RIGHT, and CENTER
wraplength Limit the text of each line. After the box text reaches the limit of characters, wrap the line
underline The underscore value is the underlined string index. If it is 0, the first character is underlined, if it is 1, the second character is underlined, and so on
.config(state=) The value can be DISABLED, NORMAL, or ACTIVE

3. Common methods of Radiobutton single box

methods role
deselect() Turn the radio button
flash() The wireless dot button flashes several times between the active and normal colors
invoke() Invokes any procedures associated with a Radiobutton state change
select() Select Radibutton
cget() Gets selected information

4. Test the cat

Examples of Radiobutton related properties are as follows

  1. Start by creating the survey file/gender label components

    Pack (side=" top") Label(self,text= "gender: "). Pack (side="left")Copy the code
  2. Create two Radiobutton checkboxes

    Self. r1 = Radiobutton(self,text = "男") self.r2 = Radiobutton(self,text = "女")Copy the code
  3. Create the Radiobutton menu and we will see that they are all selected and not in the same group

    🔔 uses a container of type StringVar to bind the Radiobutton value bidirectionally

    • The Radiobutton value option changes, and the StringVar variable changes dynamically
    • As the value of the StringVar variable changes, so does the Radiobutton direct
    Self. V = StringVar() self.v ("Fmale") self.r1 = Radiobutton(self,text = "Male", value = "Male",variable = self.v) self.r2 = Radiobutton(self,text = "Fmale", value = "Fmale",variable = self.v)Copy the code
  4. You can use the get method provided by stringVar to get the selected value

    Def confirm(self): messagebox.showinfo(" prompt "," select gender: "+ self.v.et ())Copy the code
  5. Use GUI object-oriented programming notation, use Application(Frame) to create the instance object, create the component method wrapped in createWidgetRadiobutton() method, through the constructor to create the component

  6. The complete code is as follows:

    from tkinter import * class Application(Frame): def __init__(self,master=None): # super() represents the definition of the parent class, Rather than a superclass object super () __init__ (master) self. Master = master self. The pack () the self. CreateWidgetRadiobuton (def) createWidgetRadiobuton(self): Label(self,text= "survey "). Pack (side =" top") ").pack(side="left") self.v = StringVar() self.v ("Fmale") self.r1 = Radiobutton(self,text =" male",value = "Male",variable = self.v) self.r2 = Radiobutton(self,text = "female ",value = "Fmale",variable = self.v) self.r1.pack(side = "left"); Self.r2. pack(side =" left") Button(self, text=" confirm ", command=self.confirm, anchor="w").pack(side="left") def confirm(self): Messagebox.showinfo (" hint "," select gender: "+self.v.get()) root = Tk() root.geometry("400x130+200+300") root.title("MyfirstAPP") app = Application(master=root) root.mainloop()Copy the code

conclusion

In this installment, we’ll learn about the Radiobutton checkbox component, which you can use when a form only needs radio content.

The above is the content of this issue, welcome to the big people like the comments and corrections, see next time ~ღ(´ ᴗ · ‘) than the heart 🌹🌹 ᴗ Stan ღ