【问题标题】:Can you update the text and value of a radio button in tkinter?您可以更新 tkinter 中单选按钮的文本和值吗?
【发布时间】:2014-11-07 04:36:14
【问题描述】:

我正在使用 Python 2.7.6

我最近才开始使用 Tkinter,并且正在将命令提示符测验转变为 GUI 测验。到目前为止,除了我正在使用的单选按钮之外,我已经成功了。我可以创建一组单选按钮并在需要时提取它们的值,但我不确定在单击“下一步”按钮后如何更新单选按钮的文本和值。

这不是我的全部代码,因为它超过 800 行,但在这里我创建了单选按钮和函数def Guide(): 的淡化版本,我想在其中更新单选按钮。

假设Guide_list[Current].R1Guide_list[Current].V1 分别检索到正确的字符串和整数。

“当前”是我的计数器。

from Tkinter import *
app=Tk()

Current=0
R=IntVar()
R.set(None)
Resp1=Radiobutton(app, text=Guide_list[Current].R1, value=Guide_list[Current].V1,variable=R).pack(anchor=W)
Resp2=Radiobutton(app, text=Guide_list[Current].R2, value=Guide_list[Current].V2,variable=R).pack(anchor=W)
Resp3=Radiobutton(app, text=Guide_list[Current].R3, value=Guide_list[Current].V3,variable=R).pack(anchor=W)
Resp4=Radiobutton(app, text=Guide_list[Current].R4, value=Guide_list[Current].V4,variable=R).pack(anchor=W)
Resp5=Radiobutton(app, text=Guide_list[Current].R5, value=Guide_list[Current].V5,variable=R).pack(anchor=W)
Resp6=Radiobutton(app, text="N/A", value="N/A",variable=R).pack(anchor=W)
def Guide():
    global Current
    Current=Current+1
    ### Insert code to update the radio buttons ###

button2=Button(app,text="Next", width=15,command=Guide)
button2.pack(side='bottom')

app.mainloop()

【问题讨论】:

  • 您好,请问您是否正在使用任何教程来学习 Tkinter?我看到很多人都在使用相同的“assignment+pack 反模式”,我正试图找出他们都是从哪里得到它的。
  • 我正在使用我在 Youtube 上找到的各种教程。

标签: python-2.7 tkinter radio-button


【解决方案1】:

您可以使用config 方法修改小部件的属性。

    def Guide():
        global Current
        Current=Current+1
        Resp1.config(text="Hello")

但是,只有当您仍然有对您尝试配置的小部件的现有引用时,您才能执行此操作。正如您现在的代码一样,您没有这个; Resp1 到 Resp6 都是 None,因为您将它们指向 .pack 的返回值,而不是实际的 Radiobuttons。另见Why is None returned instead of tkinter.Entry object。您需要单独打包。

Resp1=Radiobutton(app, text=Guide_list[Current].R1, value=Guide_list[Current].V1,variable=R)
Resp2=Radiobutton(app, text=Guide_list[Current].R2, value=Guide_list[Current].V2,variable=R)
Resp3=Radiobutton(app, text=Guide_list[Current].R3, value=Guide_list[Current].V3,variable=R)
Resp4=Radiobutton(app, text=Guide_list[Current].R4, value=Guide_list[Current].V4,variable=R)
Resp5=Radiobutton(app, text=Guide_list[Current].R5, value=Guide_list[Current].V5,variable=R)
Resp6=Radiobutton(app, text="N/A", value="N/A",variable=R)

Resp1.pack(anchor=W)
Resp2.pack(anchor=W)
Resp3.pack(anchor=W)
Resp4.pack(anchor=W)
Resp5.pack(anchor=W)
Resp6.pack(anchor=W)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-12
    • 1970-01-01
    • 2016-12-23
    • 1970-01-01
    相关资源
    最近更新 更多