【问题标题】:How to disable multiple radiobuttons in Python?如何在 Python 中禁用多个单选按钮?
【发布时间】:2018-08-10 05:22:58
【问题描述】:

我的程序的这一部分允许用户选择从 1 到 5 的数字,并提供所有选择的数字的总和。我想在总和达到 30 时禁用单选按钮。我该怎么做?

total_choices = [("1"),
                 ("2"),
                 ("3"),
                 ("4"),
                 ("5")]
var = tk.IntVar()
var.set(0)  

sum = 0
def Calculator():
    global sum
    num = var.get()
    sum = sum + num
    if sum>30
       # Grey out all the radio buttons

for val, choice in enumerate(total_choices):
    tk.Radiobutton(root,
                   text=choice,
                   indicatoron = 0,
                   width = 10,
                   variable=var,
                   command=Calculator,
                   value=val).place(x=val*100,y=180)

【问题讨论】:

  • 保留对每个单选按钮的引用,并将每个单选按钮的状态设置为"disabled"
  • 我试图在另一个我有很多按钮的程序中做同样的事情。我将不得不单独引用它们,这很麻烦。有什么办法可以使用一维数组来改变它们的所有状态?例如, btn[num].config(state=DISABLED) 当我尝试它时,它给了我一个“Nonetype”错误
  • 你可以使用一维数组吗?当然。小部件对象没有什么特别之处。它与任何其他类型的对象没有什么不同。至于 NoneType 错误,这个问题已经在这个网站上被询问和回答了大约 1000 次。

标签: python tkinter radio-button


【解决方案1】:

您可以通过从其父母的孩子列表中查找单选按钮来禁用它们:

try:                        # In order to be able to import tkinter for
    import tkinter as tk    # either in python 2 or in python 3
except ImportError:
    import Tkinter as tk


def disable_multiple_radiobuttons():
    global total
    print(repr(sum_label['text']))
    total += var.get()
    sum_label['text'] = total
    if total >= 30:
        for child in root.winfo_children():
            if child.winfo_class() == 'Radiobutton':
                child['state'] = 'disabled'


root = tk.Tk()
var = tk.IntVar(value=1)
total = 0
sum_label = tk.Label(root, text=total)
sum_label.pack()
for i in range(1, 6):
    tk.Radiobutton(root, text=i, variable=var, value=i,
        command=disable_multiple_radiobuttons).pack()
tk.mainloop()

或者您可以将它们放在一个集合类型中,然后轻松地禁用它们:

try:                        # In order to be able to import tkinter for
    import tkinter as tk    # either in python 2 or in python 3
except ImportError:
    import Tkinter as tk


def on_rb_press():
    sum_label['text'] += var.get()
    if sum_label['text'] >= 30:
        for key in radiobuttons:
            radiobuttons[key]['state'] = 'disabled'


root = tk.Tk()
sum_label = tk.Label(root, text=0)
sum_label.pack()
radiobuttons = dict()
var = tk.IntVar(value=1)
for i in range(1, 6):
    radiobuttons[i] = tk.Radiobutton(root, text=i, variable=var,
                                                value=i, command=on_rb_press)
    radiobuttons[i].pack()
tk.mainloop()

【讨论】:

  • 第一个选项有效。第二个选项给了我以下错误:Tkinter 回调 Traceback 中的异常(最近一次调用最后一次):文件“C:\Python27\lib\lib-tk\Tkinter.py”,第 1532 行,在 call return self.func(*args) File "C:/Python27/totaler.py", line 15, in on_rb_press radiobuttons[key]['state'] = "DISABLED" TypeError: 'NoneType' object does not support item assignment
【解决方案2】:

我会将所有单选按钮存储在一个列表中,然后在达到 30 个时禁用每个按钮,如下所示:

total_choices = [("1"),
                 ("2"),
                 ("3"),
                 ("4"),
                 ("5")]
var = tk.IntVar()
var.set(0)  

sum = 0
def Calculator():
    global sum
    global buttons
    num = var.get()
    sum = sum + num
    if sum>30
       # Grey out all the radio buttons
        for b in buttons:
            b.config(state=disabled)

global buttons
buttons = list()
for val, choice in enumerate(total_choices):
    buttons.append(tk.Radiobutton(root,
        text=choice,
        indicatoron = 0,
        width = 10,
        variable=var,
        command=Calculator,
        value=val))
    buttons[-1].place(x=val*100,y=180))

【讨论】:

  • Tkinter 回调 Traceback 中的异常(最近一次调用最后一次):call 中的文件“C:\Python27\lib\lib-tk\Tkinter.py”,第 1532 行return self.func(*args) File "C:/Python27/totaler.py", line 23, in Calculator b.config(state=disabled) AttributeError: 'NoneType' object has no attribute 'config' 再次出现相同的错误消息。顺便说一句,我使用的是 Python 2.7.9。我不知道是方法错误还是因为版本不同导致语法错误
猜你喜欢
  • 1970-01-01
  • 2020-09-18
  • 2021-07-30
  • 1970-01-01
  • 2018-08-28
  • 1970-01-01
  • 1970-01-01
  • 2022-08-14
  • 2021-12-05
相关资源
最近更新 更多