【发布时间】:2017-11-19 05:48:41
【问题描述】:
这是按下打开新窗口的按钮时出现的错误。
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 1699, in __call__
return self.func(*args)
File "/Users/Jaguar/PycharmProjects/learnpython/chemcalc.py", line 78, in <lambda>
Button(master, text='Sub', width=3, command=lambda: self.sub_win()).grid(row=2, column=6)
File "/Users/Jaguar/PycharmProjects/learnpython/chemcalc.py", line 57, in sub_win
top = Toplevel(self)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 2336, in __init__
BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 2286, in __init__
BaseWidget._setup(self, master, cnf)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 2256, in _setup
self.tk = master.tk
AttributeError: 'calc' object has no attribute 'tk'
我的代码:
from tkinter import *
class calc:
def sub_win(self):
top = Toplevel(self)
def __init__(self, master):
master.title('Title')
master.geometry()
self.e = Entry(master, justify = RIGHT)
self.e.grid(row=0, column=0, columnspan=8, pady=3)
self.answerlist = []
root = Tk()
obj=calc(root)
root.mainloop()
【问题讨论】:
-
请花时间阅读:stackoverflow.com/help/how-to-ask。当我们能够理解问题时,使用它会更容易提供帮助。
-
总是放完整的错误信息(Traceback)。还有其他有用的信息。你可以直接创建
Toplevel- 没有类吗? -
顺便说一句:你执行
calc()但你没有__init__(self, parent)和Toplevel使用self(这不是 tkinter 小部件)但它需要root。 -
我没有发布完整的代码,我有 __init__(self, master)
-
您的代码有效。错误信息与您分享的代码不符。
标签: python tkinter tk toplevel