我无法使用 Toplevel 打开新的 window
I'm having trouble getting a new window to open with Toplevel
这是我在按下打开新按钮 window 时遇到的错误。
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()
您的问题是 self
Toplevel(self)
大多数小部件需要 parent 作为第一个参数,必须是 tkinter 小部件。
在你的代码中 self
不是 tkinter 小部件。
您必须像在 Entry(master)
中那样使用 master
top = Toplevel(master)
但您必须将 master
保持为 self.master
。
Toplevel
也可以 运行 没有 parent
top = Toplevel()
它会将 Tk()
用作 parent。
这是我在按下打开新按钮 window 时遇到的错误。
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()
您的问题是 self
Toplevel(self)
大多数小部件需要 parent 作为第一个参数,必须是 tkinter 小部件。
在你的代码中 self
不是 tkinter 小部件。
您必须像在 Entry(master)
master
top = Toplevel(master)
但您必须将 master
保持为 self.master
。
Toplevel
也可以 运行 没有 parent
top = Toplevel()
它会将 Tk()
用作 parent。