Python 如何在TKinter中打开新窗口,然后向其中添加按钮?

Python 如何在TKinter中打开新窗口,然后向其中添加按钮?,python,tkinter,Python,Tkinter,我试图在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

我试图在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()

这里的任何帮助都会很棒

您不能将一个类作为另一个窗口的主窗口传递,就像您在这里所做的那样:

numberGenButton = Button(MainWindow, text="Number Generator")
timeWindowButton = Button(MainWindow, text="Clock")
passwordGeneratorButton = Button(MainWindow, text="Generate a password")
除了根小部件之外,每个小部件都需要一些其他小部件作为其主部件。您需要使用实例,在本例中为
self

numberGenButton = Button(self, text="Number Generator")
timeWindowButton = Button(self, text="Clock")
passwordGeneratorButton = Button(self, text="Generate a password")

我不明白的是,您使用
master.mainloop()
只要
mainloop
就足够了,要创建一个新窗口,就这样做吧

root = Tk()
window = Tk()

如果我误解了问题,请纠正我。

请将您的问题包括错误。@BryanOakley编辑!对不起,这么说也没什么不对的
root = Tk()
window = Tk()