[파이썬] tkinter 라디오 버튼(`Radiobutton`) 위젯

In this blog post, we will explore the tkinter Radiobutton widget in Python. Radiobuttons are a type of GUI widget that allow users to select one option from a set of mutually exclusive choices. They are typically used when there is a need to select a single option from a list of options.

What is a Radiobutton?

A Radiobutton is a graphical user interface element that presents a set of options to the user. The options are typically presented as text or images, and the user can only select one option at a time. When one Radiobutton is selected, the others in the same group are automatically deselected. This behavior makes Radiobuttons useful for scenarios where only one option can be chosen.

Creating a Radiobutton Widget in tkinter

To create a Radiobutton widget in tkinter, you need to follow these steps:

  1. Import the tkinter module: import tkinter as tk
  2. Create an instance of the Tk class to represent the main window: root = tk.Tk()
  3. Create a variable to hold the selected option: selected_option = tk.StringVar()
  4. Create Radiobuttons with the Radiobutton class, passing the window instance, a text label, the value of the option, the variable holding the selected option, and the command to be executed when the Radiobutton is selected:
     option1 = tk.Radiobutton(root, text="Option 1", value="option1", variable=selected_option)
     option2 = tk.Radiobutton(root, text="Option 2", value="option2", variable=selected_option)
     option3 = tk.Radiobutton(root, text="Option 3", value="option3", variable=selected_option)
    
  5. Pack or grid the Radiobuttons to display them on the window:
     option1.pack()
     option2.pack()
     option3.pack()
    
  6. Run the main event loop: root.mainloop()

Example Code

Here’s an example code snippet that demonstrates the usage of Radiobutton widget in tkinter:

import tkinter as tk

def display_selected_option():
    print(selected_option.get())

root = tk.Tk()

selected_option = tk.StringVar()

option1 = tk.Radiobutton(root, text="Option 1", value="option1", variable=selected_option, command=display_selected_option)
option2 = tk.Radiobutton(root, text="Option 2", value="option2", variable=selected_option, command=display_selected_option)
option3 = tk.Radiobutton(root, text="Option 3", value="option3", variable=selected_option, command=display_selected_option)

option1.pack()
option2.pack()
option3.pack()

root.mainloop()

In this example, we create three Radiobuttons labeled “Option 1”, “Option 2”, and “Option 3”. The selected_option variable holds the currently selected option. We also define a function display_selected_option that prints the selected option. Finally, we associate the function with each Radiobutton using the command parameter.

Conclusion

The tkinter Radiobutton widget provides a simple and intuitive way to present a set of mutually exclusive choices to the user. It allows users to select only one option from the provided choices. By following the steps outlined in this blog post, you can easily create and use Radiobuttons in your tkinter applications.