【问题标题】:tkinter radiobutton value no pass to label, remain at initial valuetkinter 单选按钮值不传递给标签,保持初始值
【发布时间】:2023-01-11 12:01:30
【问题描述】:

我尝试显示单选按钮的当前变量,但它似乎仍保持为零的初始值。

我的代码如下:

single_magnet_plot_style=IntVar(value=0)

单选按钮变量赋值:

scatter_rbtn=Radiobutton(window, text="Scatter Plot", variable=single_magnet_plot_style,value=1)
scatter_rbtn.place(relx=0.4,y=600)
        
line_rbtn=Radiobutton(window, text="Line Plot", variable=single_magnet_plot_style,value=2)
line_rbtn.place(relx=0.6, y=600)

标签显示:

new_label=Label(window, text=single_magnet_plot_style.get(), fg='red', font=("Helvetica", 13))
new_label.place(relx=0.5, y=550,anchor=CENTER)

在我的窗口,标签一直保持在 0 值。

从单选按钮值返回 new_label 有什么建议吗?

【问题讨论】:

    标签: python tkinter label


    【解决方案1】:

    如果您希望在单选按钮的选择发生更改时更新标签,则需要使用选项 textvariable 而不是 text

    new_label=Label(window, textvariable=single_magnet_plot_style, fg='red', font=("Helvetica", 13))
    

    【讨论】:

    • 感谢您的回答,它现在运行良好。
    猜你喜欢
    • 1970-01-01
    • 2013-12-24
    • 2016-10-02
    • 1970-01-01
    • 2015-09-04
    • 2012-09-11
    • 1970-01-01
    • 2011-03-13
    • 1970-01-01
    相关资源
    最近更新 更多