Tkinter 中 Checkbutton 的 select() 不起作用 (Python 3)

4 投票
1 回答
8650 浏览
提问于 2025-04-17 20:55

我正在写一个程序,这个程序会从文件中读取数据,并根据这些数据调整设置。下面是我创建复选框的代码:

should_auto = BooleanVar()    
autodetect = Checkbutton(root, text="Autodetect", var=should_auto, onvalue = True, offvalue = False, command=toggle_delay)
autodetect.grid(row=0, column=2, padx=7, pady=5, sticky=W)

接着我想要“勾选”这个复选框,使用了以下代码:

autodetect.select()

但是这段代码返回了一个错误:

Traceback (most recent call last):
  File "C:\Users\CedBook\Documents\GitHub\live-image-desktop-background\src\GUI.py", line 110, in <module>
    autodetect.select()
AttributeError: 'Checkbutton' object has no attribute 'select'

我也试过使用 autodetect.set(True),但结果几乎是同样的错误,提示 Checkbutton object has no attribute 'set'。我在effbot.org上看到过 .select() 的用法,但我不确定这是不是适用于Python 3?

1 个回答

1

你能直接设置布尔值吗?

should_auto.set(True)

这样应该会更新依赖于它的复选框。

撰写回答