我正在使用Tkinter在python中编写一个程序。我目前的问题是我有按钮打开另一个窗口。我的问题是它无法打开两个以上的窗口。关于如何解决这个问题的任何想法。
from tkinter import *
class first(object):
def __init__(self, master):
self.master = master
self.admin_btn = Button(master, text = "First", command = self.second)
self.admin_btn.grid(row = 0, column = 0)
def second(self):
self.w = second(self.master)
self.master.wait_window(self.w.top)
class second(object):
def __init__(self, master):
top = self.top = Toplevel(master)
self.second = Button(top, text = "Second", command = self.third)
self.second.grid(row= 0, column = 0)
def third(self):
self.w = third(self.master)
self.master.wait_window(self.w.top)
class third(object):
def __init__(self, master):
top = self.top = Toplevel(master)
self.second = Button(top, text = "Third")
self.second.grid(row= 0, column = 0)
if __name__ == "__main__":
root = Tk()
m = first(root)
root.mainloop()
答案 0 :(得分:0)
尝试使用此代码:
import tkinter as tk
class Demo1:
def __init__(self, master):
self.master = master
self.frame = tk.Frame(self.master)
self.button1 = tk.Button(self.frame, text = 'First', width = 25, command = self.new_window)
self.button1.pack()
self.frame.pack()
def new_window(self):
self.newWindow = tk.Toplevel(self.master)
self.app = Demo2(self.newWindow)
class Demo2:
def __init__(self, master):
self.master = master
self.frame = tk.Frame(self.master)
self.quitButton = tk.Button(self.frame, text = 'Second', width = 25, command = self.close_windows)
self.quitButton.pack()
self.frame.pack()
def close_windows(self):
self.master.destroy()
def main():
root = tk.Tk()
app = Demo1(root)
root.mainloop()
if __name__ == '__main__':
main()
在此代码中,我添加了两个窗口,但您可以添加第三个窗口。
答案 1 :(得分:0)
你在第二个窗口忘记了
self.master = master
当您尝试打开第三个窗口时收到错误消息。
但您应该将Toplevel(master)
分配给self. master
,然后使用self.master
代替self.top
from tkinter import *
class First(object):
def __init__(self, master):
self.master = master # = Tk()
self.btn = Button(self.master, text="First", command=self.second)
self.btn.grid(row=0, column=0)
def second(self):
self.w = Second(self.master)
self.master.wait_window(self.w.master)
class Second(object):
def __init__(self, master):
self.master = Toplevel(master)
self.btn = Button(self.master, text="Second", command=self.third)
self.btn.grid(row=0, column=0)
def third(self):
self.w = Third(self.master)
self.master.wait_window(self.w.master)
class Third(object):
def __init__(self, master):
self.master = Toplevel(master)
self.btn = Button(self.master, text="Third")
self.btn.grid(row=0, column=0)
if __name__ == "__main__":
root = Tk()
first = First(root)
root.mainloop()
现在所有课程都非常相似 - 他们有self.master = Tk()
或self.master = Toplevel(master)
顺便说一句:对类使用CamelCase
名称 - First
,Second
,Third
- 它有助于识别代码中的类。
答案 2 :(得分:0)
我建议你所有的类继承自Toplevel而不是object,这样,你的所有类都会有master
属性(这就是你有错误的原因,second
类没有一个):
from tkinter import *
class First(Toplevel):
def __init__(self, master):
Toplevel.__init__(self, master)
self.admin_btn = Button(self, text = "First", command = self.second)
self.admin_btn.grid(row=0, column=0)
def second(self):
self.w = Second(self)
self.wait_window(self.w)
class Second(Toplevel):
def __init__(self, master):
Toplevel.__init__(self, master)
self.second = Button(self, text = "Second", command = self.third)
self.second.grid(row=0, column=0)
def third(self):
self.w = Third(self.master)
self.wait_window(self.w)
class Third(Toplevel):
def __init__(self, master):
Toplevel.__init__(self, master)
self.second = Button(self, text = "Third")
self.second.grid(row=0, column=0)
if __name__ == "__main__":
root = Tk()
m = First(root)
m.lift(root)
root.wait_window(m)
root.mainloop()