Python graphical interface basics: adding radio buttons (Radiobutton) to the Tkinter window

introduction

In this article on the basics of Python graphical interface, we will focus on how to add radio buttons ( Radiobutton ) in Tkinter . A radio button is a GUI element used to select an option . Radio buttons are useful whether they are used to set application options, make individual selections, or filter data. In this article, we will explain in detail how to add radio buttons in a Tkinter window and how to get the selection made by the user.

What is a Tkinter radiobutton?

Tkinter 's radiobutton is a GUI element used to select an option . Radio buttons are typically used for a set of mutually exclusive options, of which the user can only select one. Typically, radio buttons group together a group of related options, allowing the user to select one of the options, while selecting other options automatically cancels the previous selection.

Let's start learning how to add radio buttons in Tkinter window.

Step 1: Import the Tkinter module

First, make sure you have Python installed and include the Tkinter library. Then, import the Tkinter module in your Python script to use the functionality of the Tkinter library.

import tkinter as tk

Step 2: Create Tkinter window

Before using Tkinter , you need to create a Tkinter window object, usually called root. This window will serve as the main window of the GUI application.

root = tk.Tk()

Step 3: Create a radio button (Radiobutton)

To create a radio button, you can use Radiobuttonthe class. Here's an example of creating a simple radio button:

# 创建一个StringVar变量以存储单选按钮的值
radio_var = tk.StringVar()

# 创建单选按钮
radio_button1 = tk.Radiobutton(root, text="选项1", variable=radio_var, value="选项1")
radio_button2 = tk.Radiobutton(root, text="选项2", variable=radio_var, value="选项2")

In the above example, we created a StringVarvariable of type radio_varto store the value of the radio button. We then created two radio buttons radio_button1and radio_button2, attached them to rootthe window, and set the text and value of each radio button.

Step 4: Get the value of the radio button

To get the value of a radio button selected by the user, you can use get()the method to access the variable associated with the radio button. Here is an example:

selected_option = radio_var.get()

In this example, we use get()the method to get the value of the radio button selected by the user and store it in the variable selected_option.

Step 5: Add radio buttons to the window

Once the radio buttons are created, pack()they need to be added to the window using the method. This will determine the position of the radio button in the window.

radio_button1.pack()
radio_button2.pack()

pack()Method automatically adjusts the radio button's position based on the window's size and contents.

Complete sample code

Here is a complete sample code that demonstrates how to create a Tkinter window and add two radio buttons to it, and get the user-selected option when the button is clicked:

import tkinter as tk

# 创建Tkinter窗口
root = tk.Tk()
root.title("Tkinter单选按钮示例")

# 创建一个StringVar变量以存储单选按钮的值
radio_var = tk.StringVar()

# 创建单选按钮1
radio_button1 = tk.Radiobutton(root, text="选项1", variable=radio_var, value="选项1")

# 创建单选按钮2
radio_button2 = tk.Radiobutton(root, text="选项2", variable=radio_var, value="选项2")

# 创建按钮点击事件处理程序
def button_click():
    selected_option = radio_var.get()
    label.config(text="你选择的选项是:" + selected_option)

# 创建按钮
button = tk.Button(root, text="获取选择", command=button_click)

# 创建标签
label = tk.Label(root, text="")

# 将单选按钮、按钮和标签添加到窗口
radio_button1.pack()
radio_button2.pack()
button.pack()
label.pack()

# 启动Tkinter主事件循环
root.mainloop()

Code explanation

Let us explain the above code line by line:

  • First, we imported the Tkinter module in order to use the functionality of the Tkinter library.

  • Next, we created a Tkinter window object rootand set the title of the window to " Tkinter Radio Button Example".

  • We create a StringVarvariable of type radio_varto store the value of the radio button.

  • We then created two radio buttons radio_button1and radio_button2, attached them to rootthe window, and set the text and value of each radio button.

  • We define a button_clickbutton click event handler named which will be executed when the button is clicked. In this example, we use get()the method to get the value of the radio button selected by the user and update the label's text based on the value.

  • We create a button button, set the text on the button to "Get Selection", and button_clickassociate an event handler with the button's click event.

  • Finally, we create a label labelthat displays the options selected by the user.

  • We pack()added radio buttons, buttons, and labels to the window using the method and started Tkinter 's main event loop.

Customize radio button properties

In addition to basic radio buttons, you can customize the appearance and behavior of radio buttons. You can set the font, text color, background color, response function when selected, etc. of the radio button. Here is an example that demonstrates how to customize the properties of a radio button:

# 创建一个自定义样式的单选按钮
custom_radio_button = tk.Radiobutton(
    root,
    text="自定义选项",
    font=("Helvetica", 12),    # 设置字体和字号
    fg="green",                # 设置文本颜色
    bg="lightgray",            # 设置背景颜色
    selectcolor="red",         # 设置选中时的颜色
    command=custom_function    # 设置单选按钮选中时的响应函数
)

# 将自定义单选按钮添加到窗口
custom_radio_button.pack()

In the above example, we created a custom-styled radio button, setting the font, text color, background color, selected color, and selected response function.

in conclusion

In this article, we learned how to add radio buttons to a Tkinter window and how to get the selection made by the user. Radio buttons are a commonly used element in GUI applications to provide a set of mutually exclusive options. By creating and customizing radio buttons, you can add more interactivity and functionality to your application. In the following tutorials, we will continue to learn how to add additional GUI elements, handle different types of events, and build richer and more powerful GUI applications. Keep learning and you'll be able to create more interesting and useful GUI applications!

Guess you like

Origin blog.csdn.net/qq_38161040/article/details/132839458