【发布时间】:2020-11-27 23:56:53
【问题描述】:
我正在尝试在 Tkinter 中打开一个新窗口,然后向其中添加按钮。 新窗口打开没有问题,但是当我尝试向其添加按钮时出现 AttributeError。
这是我当前的代码:
from tkinter import *
from tkinter.ttk import *
import datetime
import time
import random
import json
class MainWindow(Toplevel):
def __init__(self, master = None):
super().__init__(master = master)
self.title("Jarvis")
self.geometry("500x500")
label = Label(self, text="Please choose one of the options below")
label.pack()
numberGenButton = Button(MainWindow, text="Number Generator")
numberGenButton.pack()
timeWindowButton = Button(MainWindow, text="Clock")
timeWindowButton.pack()
passwordGeneratorButton = Button(MainWindow, text="Generate a password")
passwordGeneratorButton.pack()
master = Tk()
master.geometry("500x500")
welcome = Label(text="Welcome to Jarvis")
welcome.pack()
Label(text="Please note closing this window will close Jarvis!").pack()
getStarted = Button(master, text="Get Started")
getStarted.bind("<Button>", lambda e: MainWindow(master))
getStarted.pack()
master.mainloop()
错误:
File "c:\Program Files\Python38\lib\tkinter\ttk.py", line 612, in __init__
Widget.__init__(self, master, "ttk::button", kw)
File "c:\Program Files\Python38\lib\tkinter\ttk.py", line 557, in __init__
tkinter.Widget.__init__(self, master, widgetname, kw=kw)
File "c:\Program Files\Python38\lib\tkinter\__init__.py", line 2561, in __init__
BaseWidget._setup(self, master, cnf)
File "c:\Program Files\Python38\lib\tkinter\__init__.py", line 2530, in _setup
self.tk = master.tk
AttributeError: type object 'MainWindow' has no attribute 'tk'
这里的任何帮助都会很棒!
【问题讨论】:
-
请edit您的问题包含错误。
-
@BryanOakley 已编辑!对不起那个伙伴