The Radiobutton (sometimes called option button) is a standard Tkinter widget used to implement one-of-many selections. Radiobuttons can contain text or images, and we can associate a Python function or method with each button. When the button is pressed, Tkinter automatically calls that function or method.
The button can only display text in a single font, but the text may span more than one line. In addition, one of the characters can be underlined, for example to mark a keyboard shortcut. By default, the Tab key can be used to move to a button widget.
Each group of Radiobutton widgets should be associated with single variable. Each button then represents a single value for that variable. Pushing a button changes the value of this variable to a predefined certain value.
The Radiobutton widget is very similar to the check button we discussed in the previous post. To get a proper radio behavior, make sure to have all buttons in a group point to the same variable, and use the value option to specify what value each button represents. The following program creates a radio button:
In the above example we created buttons for two programming languages, but in case we need more options then the approach followed in the previous example for creating buttons would be time consuming and thus using a loop to create buttons would be a better option. The following example shows this approach:
import tkinter as tk
from tkinter import *
root = tk.Tk()
v = StringVar()
v.set("L") # initialize
Languages = [
("Python", "1"),
("Java", "L"),
("C#", "2"),
("C++", "3"),
]
for text, mode in Languages:
b = Radiobutton(root, text=text,variable=v, value=mode)
b.pack(anchor=W)
mainloop()
The output of the program is shown below:
Here I am ending today's post. Make some programs on your own as GUI needs a lot practice. Till we meet next keep practicing and learning Python as Python is easy to learn!
The button can only display text in a single font, but the text may span more than one line. In addition, one of the characters can be underlined, for example to mark a keyboard shortcut. By default, the Tab key can be used to move to a button widget.
Each group of Radiobutton widgets should be associated with single variable. Each button then represents a single value for that variable. Pushing a button changes the value of this variable to a predefined certain value.
The Radiobutton widget is very similar to the check button we discussed in the previous post. To get a proper radio behavior, make sure to have all buttons in a group point to the same variable, and use the value option to specify what value each button represents. The following program creates a radio button:
import tkinter as tk
root = tk.Tk()
v = tk.IntVar()
tk.Label(root,
text="""Your Preference:""",
justify = tk.LEFT,
padx = 20).pack()
tk.Radiobutton(root,
text="Python",
padx = 20,
variable=v,
value=1).pack(anchor=tk.W)
tk.Radiobutton(root,
text="Java",
padx = 20,
variable=v,
value=2).pack(anchor=tk.W)
root.mainloop()
The output is shown below:
In the above example we created buttons for two programming languages, but in case we need more options then the approach followed in the previous example for creating buttons would be time consuming and thus using a loop to create buttons would be a better option. The following example shows this approach:
import tkinter as tk
from tkinter import *
root = tk.Tk()
v = StringVar()
v.set("L") # initialize
Languages = [
("Python", "1"),
("Java", "L"),
("C#", "2"),
("C++", "3"),
]
for text, mode in Languages:
b = Radiobutton(root, text=text,variable=v, value=mode)
b.pack(anchor=W)
mainloop()
The output of the program is shown below:
0 comments:
Post a Comment