TKinter 8.5 中的布尔变量 [英] Boolean Variable in TKinter 8.5
问题描述
我在 python shell 中做了这个:
from Tkinter import *v = BooleanVar()
但我收到以下错误:
回溯(最近一次调用最后一次):文件<pyshell#52>",第 1 行,在 <module> 中v = BooleanVar()文件/usr/lib/python2.7/lib-tk/Tkinter.py",第 320 行,在 __init__ 中Variable.__init__(self, master, value, name)文件/usr/lib/python2.7/lib-tk/Tkinter.py",第 185 行,在 __init__ 中self._tk = master.tkAttributeError: 'NoneType' 对象没有属性 'tk'>>>
然后在尝试让它发挥作用的同时,我尝试了这个:
v = BooleanVar
这奏效了.
所以我继续做下一个合乎逻辑的事情,检查变量是否已经初始化并得到这个:
<预><代码>>>>v然后我尝试使用值 True
进行初始化,为此我使用了 set
方法:
v.set(True)
我收到以下错误:
回溯(最近一次调用最后一次):文件<pyshell#63>",第 1 行,在 <module> 中v.set(真)类型错误:必须使用 BooleanVar 实例作为第一个参数调用未绑定的方法 set()(改为使用 bool 实例)
发生了什么?
请帮我解决这个问题
目标我想将其用作与菜单中的复选按钮相关联的变量
规格 Linux mint 14、python 2.7、Tkinter 8.5
简答:首先调用 root = tk.Tk()
.
长答案:引发错误的行,
self._tk = master.tk
失败,因为 master
是 None
.但是,请查看此行上方的代码(在 Tkinter.py 中):
class 变量:def __init__(self, master=None, value=None, name=None):如果不是大师:主 = _default_root...self._tk = master.tk
如果您显式地将 master
小部件传递给 BooleanVar
(它是 Variable
的子类),则 master
不会是 None
.或者,如果 _default_root
不是 None
,那么 master
不会是 None
.
在普通的 Tkinter 应用程序中,您将在制作 BooleanVar
之前制作一个根窗口.在这种情况下,tk.BooleanVar()
不会引发错误.
root = tk.Tk()
将 _default_root
全局变量设置为自身(只要 useTk
参数为 True
-- 默认情况下是这样).如果没有使用 tk.BooleanVar(master)
显式设置主控,_default_root
被 BooleanVar
用作小部件的主控.
总而言之,在调用 tk.BooleanVar()
之前,要么调用 root = tk.Tk()
或类似的东西来设置 _default_root
>,或者传递一个显式主控件作为第一个参数:tk.BooleanVar(master)
.
在[1]中:将Tkinter导入为tk在 [2] 中:root = tk.Tk()在 [3] 中:x = tk.BooleanVar()在 [4]: x.set(True)在 [5] 中:x.get()出[5]:1
I did this inside the python shell:
from Tkinter import *
v = BooleanVar()
But I got the following error:
Traceback (most recent call last):
File "<pyshell#52>", line 1, in <module>
v = BooleanVar()
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 320, in __init__
Variable.__init__(self, master, value, name)
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 185, in __init__
self._tk = master.tk
AttributeError: 'NoneType' object has no attribute 'tk'
>>>
And then while just playing around trying to make it work I tried this:
v = BooleanVar
And this worked.
So I went on to do the next logical thing, checking if the variable has been initialised and got this:
>>> v
<class Tkinter.BooleanVar at 0xb6f666bc>
Then I tried to initialise with the value True
and for that I used the set
method:
v.set(True)
For which I got the following error:
Traceback (most recent call last):
File "<pyshell#63>", line 1, in <module>
v.set(True)
TypeError: unbound method set() must be called with BooleanVar instance as first argument (got bool instance instead)
What is going on?
Please help me with this issue
Goal I want to use this as the variable associated with a check button in a menu
specs Linux mint 14, python 2.7, Tkinter 8.5
Short answer: Call root = tk.Tk()
first.
Long answer: The line that is raising the error,
self._tk = master.tk
is failing because master
is None
. However, look at the code (in Tkinter.py) above this line:
class Variable:
def __init__(self, master=None, value=None, name=None):
if not master:
master = _default_root
...
self._tk = master.tk
If you explicitly pass a master
widget to BooleanVar
(which is a subclass of Variable
) then master
would not be None
. Or, if _default_root
were not None
, then master
would not be None
.
In a normal Tkinter application, you will make a root window before making a BooleanVar
. In that case, tk.BooleanVar()
will not raise an error.
root = tk.Tk()
sets the _default_root
global variable to itself (as long as the useTk
parameter is True
-- which it is by default). The _default_root
is used by BooleanVar
as the widget's master if no master is explicitly set with tk.BooleanVar(master)
.
So in summary, either call root = tk.Tk()
or something similar to set the _default_root
before calling tk.BooleanVar()
, or pass an explicit master widget as the first argument: tk.BooleanVar(master)
.
In [1]: import Tkinter as tk
In [2]: root = tk.Tk()
In [3]: x = tk.BooleanVar()
In [4]: x.set(True)
In [5]: x.get()
Out[5]: 1
这篇关于TKinter 8.5 中的布尔变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!