You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为什么Tkinter程序启动时两个radio buttons都处于选中状态?

问题现象

我刚接触Tkinter的按钮与radio buttons组件,我的实验指导老师提供了如下Python代码,该代码基于Tkinter实现打印选中的单选按钮值的功能。但我运行程序时发现两个单选按钮都处于选中状态,请问该如何解决该问题?

原有代码:

import tkinter as tk

window = tk.Tk()
window.title('Programming for Data Science')
# width x height + x_offset + y_offset:
window.geometry("500x250+100+100")

# Set font
myFont = "Arial, 16"
# Add a label
lbl_header = tk.Label(text="A Simple GUI App", font=myFont, height=1)
lbl_header.place(x=150, y=10)
# Add label
lbl = tk.Label(text="Select a colour: ", fg="navy", anchor="w", width=25, height=1, font=myFont)
lbl.place(x=10, y=50)
# Add variable var and 2 radio buttons
var = tk.StringVar()
rb1 = tk.Radiobutton(text="red", variable=var, value='r', font=myFont)
rb1.place(x=180, y=50)
rb1.deselect()

rb2 = tk.Radiobutton(text="blue", variable=var, value='b', font=myFont)
rb2.place(x=250, y=50)
rb2.deselect()
# Label to display output when button is clicked
lb_output = tk.Label(text="", fg="navy", anchor="w", width=25, height=1, font=myFont)
lb_output.place(x=10, y=100)


#################################

def select_item():
    selected = var.get()
    if selected == 'r':
        output = 'Red selected'
    elif selected == 'b':
        output = 'Blue selected'
    else:
        output = "Please select a colour"

    lb_output.config(text=output)
#################################


# Add button
button = tk.Button(text="Run", fg="black", bg="lightblue", width=10, height=1, font=myFont, command=select_item)
button.place(x=10, y=150)

window.mainloop()
问题原因
  • 两个单选按钮未显式指定父容器,Tkinter内部识别异常,没有将两个按钮判定为同一组互斥选项
  • 手动调用deselect()方法打乱了按钮和关联变量的状态同步逻辑
  • 绑定的StringVar变量没有设置初始值,触发初始显示异常
修复方案

只需要修改三处代码即可:

  1. 给两个Radiobutton的构造参数里添加父容器window
  2. 删除两行deselect()调用,单选按钮的选中状态由关联变量自动控制
  3. 给StringVar设置初始空值,保证初始状态无选中项
修复后完整代码
import tkinter as tk

window = tk.Tk()
window.title('Programming for Data Science')
# width x height + x_offset + y_offset:
window.geometry("500x250+100+100")

# Set font
myFont = "Arial, 16"
# Add a label
lbl_header = tk.Label(text="A Simple GUI App", font=myFont, height=1)
lbl_header.place(x=150, y=10)
# Add label
lbl = tk.Label(text="Select a colour: ", fg="navy", anchor="w", width=25, height=1, font=myFont)
lbl.place(x=10, y=50)
# Add variable var and 2 radio buttons,设置初始空值
var = tk.StringVar(value="")
# 给Radiobutton添加父容器window
rb1 = tk.Radiobutton(window, text="red", variable=var, value='r', font=myFont)
rb1.place(x=180, y=50)

rb2 = tk.Radiobutton(window, text="blue", variable=var, value='b', font=myFont)
rb2.place(x=250, y=50)
# Label to display output when button is clicked
lb_output = tk.Label(text="", fg="navy", anchor="w", width=25, height=1, font=myFont)
lb_output.place(x=10, y=100)


#################################

def select_item():
    selected = var.get()
    if selected == 'r':
        output = 'Red selected'
    elif selected == 'b':
        output = 'Blue selected'
    else:
        output = "Please select a colour"

    lb_output.config(text=output)
#################################


# Add button
button = tk.Button(window, text="Run", fg="black", bg="lightblue", width=10, height=1, font=myFont, command=select_item)
button.place(x=10, y=150)

window.mainloop()

内容的提问来源于stack exchange,提问作者Fazal Mahmud Niloy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 21:15:04