I have the following code which sets up some button and radio buttons. The radio buttons (Full Tcl version info: 8.6.11) can not be clicked on my side (Python 3.9.2 on Debian Linux 11):
import tkinter as tk
from tkinter import ttk
class ComplexGUIExample(tk.Tk):
def __init__(self):
super().__init__()
self.title("Complex GUI Example")
self.geometry("400x300")
# Frame for VPN controls
vpn_frame = tk.Frame(self)
vpn_frame.pack(pady=(10, 0), padx=(10, 0), fill=tk.X)
# VPN Connection Button
button_connect_vpn = tk.Button(vpn_frame, text="Connect VPN", command=self.dummy_command)
button_connect_vpn.pack(side=tk.LEFT, padx=(10, 10))
# Check VPN Status Button
button_check_vpn = tk.Button(vpn_frame, text="Check VPN Status", command=self.dummy_command)
button_check_vpn.pack(side=tk.LEFT, padx=(10, 10))
# Frame for URL Loading Preferences (Radio Buttons)
url_pref_frame = tk.LabelFrame(self, text="URL Loading Preferences", padx=10, pady=10)
url_pref_frame.pack(pady=(20, 0), padx=(10, 0), fill=tk.X)
# Variable to track the radio button selection
self.url_loading_preference = tk.StringVar(value="Most Recent")
# Radio Buttons for URL Loading Preference
tk.Radiobutton(url_pref_frame, text="Most Recent", variable=self.url_loading_preference, value="Most Recent", command=self.on_radio_change).pack(anchor=tk.W)
tk.Radiobutton(url_pref_frame, text="Oldest", variable=self.url_loading_preference, value="Oldest", command=self.on_radio_change).pack(anchor=tk.W)
tk.Radiobutton(url_pref_frame, text="Random page", variable=self.url_loading_preference, value="Random page", command=self.on_radio_change).pack(anchor=tk.W)
def dummy_command(self):
print("Button clicked")
def on_radio_change(self):
print(f"Selected URL Loading Preference: {self.url_loading_preference.get()}")
if __name__ == "__main__":
app = ComplexGUIExample()
app.mainloop()
How do I make the radio buttons clickable ?
I have tried using some other widgets but so far I haven't been able to make it work.
You can use the grid
method instead of pack
for the radio buttons. The grid
method allows you to specify the row and column where each widget should be placed, enabling them to be positioned side-by-side.
url_pref_frame = tk.LabelFrame(self, text="URL Loading Preferences", padx=10, pady=10)
url_pref_frame.pack(pady=(20, 0), padx=(10, 0), fill=tk.X)
self.url_loading_preference = tk.StringVar(value="Most Recent")
row = 0 # Keep track of the current row for grid placement
tk.Radiobutton(url_pref_frame, text="Most Recent", variable=self.url_loading_preference, value="Most Recent", command=self.on_radio_change).grid(row=row, column=0, sticky=tk.W)
row += 1
tk.Radiobutton(url_pref_frame, text="Oldest", variable=self.url_loading_preference, value="Oldest", command=self.on_radio_change).grid(row=row, column=0, sticky=tk.W)
row += 1
tk.Radiobutton(url_pref_frame, text="Random page", variable=self.url_loading_preference, value="Random page", command=self.on_radio_change).grid(row=row, column=0, sticky=tk.W)
By using grid
and specifying the row for each radio button, you ensure they are placed horizontally within the frame, making them all clickable.