Here we create radiobuttons with list comprehension:
''' tk_RadioButton_multi3.py
explore Tkinter's Radiobutton widget
create muliple radio buttons with list comprehension
'''
try:
# Python2
import Tkinter as tk
except ImportError:
# Python3
import tkinter as tk
def click():
"""shows the value of the radio button selected"""
root.title(vs.get())
root = tk.Tk()
# create a labeled frame for the radiobuttons
# relief='groove' and labelanchor='nw' are default
labfr = tk.LabelFrame(root, text=" select one ", bd=3)
labfr.pack(padx=55, pady=10)
# use a string variable
vs = tk.StringVar()
values = ['eggs', 'spam', 'cheese', 'bacon']
# create a list of Radiobutton instances
radiobuttons = [tk.Radiobutton(labfr, text=val, value=val,
variable=vs, command=click) for val in values]
# use the pack() layout manager to position the radio buttons
[rb.pack(anchor='w') for rb in radiobuttons]
# needed, set to None or any of the radiobutton values
vs.set('spam')
# show value set
if vs:
click()
root.mainloop()
Ene Uran
Posting Virtuoso
1,830 posts since Aug 2005
Reputation Points: 676
Solved Threads: 255
Skill Endorsements: 7