Sometimes we need to select one of multiple options, then we can use the Radiobutton method in the Tkinter module to create related buttons. The created option renderings are as follows:
Its syntax format is similar to the previous method, and its related options are also similar. Let's learn this control directly through examples. The code is as follows:
importtkinterwin=tkinter.Tk()win.title(Python tutorial)#Give the window a title win.minsize(366,50)#Define a minimum size defget_data():print('The selected item is the %d item'%x .get())x=tkinter.IntVar()#Here we bind a set of radio button boxes to the same variable radio_one=tkinter.Rad iobutton(win,text=option 1,value=1,variable=x,command=get_data)radio_one.pack()radio_two=tkinter.Radiobutton(win,text=option 2,value=2,variable=x,command=get_data )radio_two.pack()win.mainloop()
The running result is:
Every time we click on an option, the button is selected, and then the selected number is output on the console. It should be noted that we use the same variable for the two Radiobuttons when defining. Through this, we can make the two The common information of the button is a piece of information. Of course, we can also implement more functions through specific functions. Here we first have a brief understanding of Radiobutton.
When we do multiple-choice questions, we will encounter multiple-choice questions. The Radio button we learned above provides us with a single-select function. So if we want to use multiple-select operations, we must use the Check button.
The display effect of the Check button is as shown in the figure:
When using the Check button, the Checkbutton method in the tkinter module is used to create related controls. Below we will also learn this button directly through examples. The code is as follows:
importtkinterwin=tkinter.Tk()win.title(Python tutorial)win.minsize(200,200)defget_data():my_str=''ifx.get()==True:my_str+=Option 1 selectednify.get()= =True:my_str+=Option 2 selectednifz.get()==True:m y_str+=Option 3 selectedntext.delete(0.0,tkinter.END)#Clear the content in the text box text.insert(tkinter.INSERT,my_str)#Insert the above information into the text box x=tkinter. BooleanVar()ckbutton_one=tkinter.Checkbutton(win,text=option 1, variable=x,command=get_data)ckbutton_one.pack()y=tkinter.BooleanVar()ckbutton_two=tkinter.Checkbutton(win,text=option2,variable=y,command=get_data)ckbutton_two.pack()z=tkinter. B ooleanVar()ckbutton_three=tkinter.Checkbutton(win,text=option 3,variable=z,command=get_data)ckbutton_three.pack()text=tkinter.Text(win,width=50,height=5)text.pack() win.mainloop()
The running result is:
In this example, we have bound three different variables to three buttons respectively. We can give corresponding information when clicking different buttons. Here we define a Boolean value selection for the button. When we select When this option is selected, the Boolean value of the option is True, and then we can output the relevant information when the option is True. If it is not selected, we can also give the corresponding information.
These two buttons can provide certain help when we improve the related functions of the interface. You can try to practice using the functions in this module. If you are interested, you can even use the content of this chapter to create a single-choice and multi-choice interest answer sheet. Or related content, the learning process is also a process of discovering interests. After learning, we must connect the knowledge we have learned together.