The CTkRadioButton
is a selection button that allows the user to choose only one option among several. It works by associating multiple buttons with the same variable, where each button represents a unique value.
Basic Example of Using CTkRadioButton
import tkinter
import customtkinter
# Function called when switching radio buttons
def radio_button_event():
print("Radio button selected, current value:", radio_variable.get())
# Initial setup
app = customtkinter.CTk()
app.geometry("300x200")
# Variable to control radio buttons
radio_variable = tkinter.IntVar(value=0)
# Creating radio buttons
radio_button_1 = customtkinter.CTkRadioButton(app, text="Option 1",
command=radio_button_event,
variable=radio_variable, value=1)
radio_button_2 = customtkinter.CTkRadioButton(app, text="Option 2",
command=radio_button_event,
variable=radio_variable, value=2)
# Positioning buttons in the interface
radio_button_1.pack(pady=10)
radio_button_2.pack(pady=10)
app.mainloop()
In the example above:
-
variable=radio_variable
: Controls which button is selected. -
value=1
andvalue=2
: Defines the value assigned to the variable when the corresponding button is selected. -
command=radio_button_event
: Function called whenever the radio button is toggled.
CTkRadioButton
Parameters
You can customize the CTkRadioButton
using the following arguments:
Argument | Description |
---|---|
master |
Parent widget, such as root, Frame, or CTkFrame. |
width |
Total widget width in pixels. |
height |
Total widget height in pixels. |
radiobutton_width |
Width of the selection button in pixels. |
radiobutton_height |
Height of the selection button in pixels. |
corner_radius |
Corner radius in pixels. |
border_width_unchecked |
Border width in "unchecked" state. |
border_width_checked |
Border width in "checked" state. |
fg_color |
Fill color of the radio button. |
border_color |
Border color of the button. |
hover_color |
Highlight color when hovering over the button. |
text_color |
Color of the button text. |
text |
Text displayed next to the radio button. |
textvariable |
A StringVar object to control text dynamically. |
font |
Text font, as a tuple (font_name, size) . |
state |
Button state: "normal" or "disabled" . |
command |
Function called when clicking the button. |
variable |
Variable to store the radio button's state. |
value |
Value assigned to the variable when the button is selected. |
CTkRadioButton
Methods
Radio buttons have useful methods for control and customization:
Method | Description |
.configure() |
Allows reconfiguring button attributes such as state , text , etc. |
.cget(attribute) |
Returns the current value of a specified attribute. |
.select() |
Selects the radio button programmatically without triggering the command. |
.deselect() |
Deselects the radio button programmatically without triggering the command. |
.invoke() |
Simulates a user click, triggering the linked command. |
Advanced Example: Enabling/Disabling Radio Buttons
import tkinter
import customtkinter
def toggle_state():
if radio_button_1.cget("state") == "normal":
radio_button_1.configure(state="disabled")
radio_button_2.configure(state="disabled")
else:
radio_button_1.configure(state="normal")
radio_button_2.configure(state="normal")
# Initial setup
app = customtkinter.CTk()
app.geometry("300x200")
radio_variable = tkinter.IntVar(value=0)
# Radio buttons
radio_button_1 = customtkinter.CTkRadioButton(app, text="Option 1", variable=radio_variable, value=1)
radio_button_2 = customtkinter.CTkRadioButton(app, text="Option 2", variable=radio_variable, value=2)
# Button to toggle state
toggle_button = customtkinter.CTkButton(app, text="Enable/Disable", command=toggle_state)
# Positioning
radio_button_1.pack(pady=10)
radio_button_2.pack(pady=10)
toggle_button.pack(pady=10)
app.mainloop()
In this example:
-
.cget("state")
checks the current state of the button. -
.configure(state="disabled")
disables the buttons. -
.configure(state="normal")
re-enables the buttons.
With these examples and details, you can use CTkRadioButton
to create more dynamic and interactive interfaces in CustomTkinter.
Copyright Statement: Unless stated otherwise, all articles are original to this site, please credit the source when sharing.
Article link:http://pybeginners.com/customtkinter/how-to-create-a-radiobutton-in-customtkinter/
License Agreement:Attribution-NonCommercial 4.0 International License