在初始化时获取 tkinter StringVar() 错误 [英] Getting tkinter StringVar() error on init

查看:61
本文介绍了在初始化时获取 tkinter StringVar() 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(Python 版本:3.1.1)

我在 tkinter 中遇到 StringVar 一个奇怪的问题.在尝试持续更新项目中的消息小部件时,我在尝试创建变量时不断收到错误消息.我跳到一个交互式 python shell 进行调查,这就是我得到的:

>>>字符串变量<class 'tkinter.StringVar'>>>>字符串变量()回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中文件C:\Python31\lib\tkinter\__init__.py",第 243 行,在 __init__ 中Variable.__init__(self, master, value, name)文件C:\Python31\lib\tkinter\__init__.py",第 174 行,在 __init__ 中self._tk = master.tkAttributeError: 'NoneType' 对象没有属性 'tk'>>>

有什么想法吗?我在 tkinter 使用中看到的每个示例都显示初始化变量时没有将任何内容发送到构造函数,因此如果我遗漏了什么,我会不知所措...

解决方案

StringVar 需要一个大师:

<预><代码>>>>字符串变量(Tk())<Tkinter.StringVar 实例在 0x0000000004435208>>>>

或更常见:

<预><代码>>>>根 = Tk()>>>字符串变量()<Tkinter.StringVar 实例在 0x0000000004435508>

当您实例化 Tk 时,会创建一个新的解释器.在此之前没有任何效果:

<预><代码>>>>从 Tkinter 导入 *>>>字符串变量()回溯(最近一次调用最后一次):文件<input>",第 1 行,在 <module> 中文件C:\Python26\lib\lib-tk\Tkinter.py",第 251 行,在 __init__ 中Variable.__init__(self, master, value, name)文件C:\Python26\lib\lib-tk\Tkinter.py",第 182 行,在 __init__ 中self._tk = master.tkAttributeError: 'NoneType' 对象没有属性 'tk'>>>根 = Tk()>>>字符串变量()<Tkinter.StringVar 实例在 0x00000000044C4408>

您发现的示例的问题在于,可能在文献中它们只显示了应该在类内部或较长程序中的部分片段,因此没有明确指出导入和其他代码.

(Python version: 3.1.1)

I am having a strange problem with StringVar in tkinter. While attempting to continuously keep a Message widget updated in a project, I kept getting an error while trying to create the variable. I jumped out to an interactive python shell to investigate and this is what I got:

>>> StringVar
<class 'tkinter.StringVar'>
>>> StringVar()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python31\lib\tkinter\__init__.py", line 243, in __init__
    Variable.__init__(self, master, value, name)
  File "C:\Python31\lib\tkinter\__init__.py", line 174, in __init__
    self._tk = master.tk
AttributeError: 'NoneType' object has no attribute 'tk'
>>>

Any ideas? Every example I have seen on tkinter usage shows initializing the variable with nothing sent to the constructor so I am at a loss if I am missing something...

解决方案

StringVar needs a master:

>>> StringVar(Tk())
<Tkinter.StringVar instance at 0x0000000004435208>
>>> 

or more commonly:

>>> root = Tk()
>>> StringVar()
<Tkinter.StringVar instance at 0x0000000004435508>

When you instantiate Tk a new interpreter is created. Before that nothing works:

>>> from Tkinter import *
>>> StringVar()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Python26\lib\lib-tk\Tkinter.py", line 251, in __init__
    Variable.__init__(self, master, value, name)
  File "C:\Python26\lib\lib-tk\Tkinter.py", line 182, in __init__
    self._tk = master.tk
AttributeError: 'NoneType' object has no attribute 'tk'
>>> root = Tk()
>>> StringVar()
<Tkinter.StringVar instance at 0x00000000044C4408>

The problem with the examples you found is that probably in the literature they show only partial snippets that are supposed to be inside a class or in a longer program so that imports and other code are not explicitly indicated.

这篇关于在初始化时获取 tkinter StringVar() 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆