在我的程序中,我从我的根 tkinter 窗口创建一个窗口,并使用 .withdraw() 函数隐藏根。当我尝试通过调用根类再次显示根窗口时,它没有显示并且我的程序退出。这是我描述问题的代码的粗略概述:
class MainGUI:
def __init__(self, master):
self.master = master
#....Create and .grid() all GUI Widgets....
# Button for switching to other window
button = Button(text="CLICKME", command=lambda: self.other_window())
# Call and define show function at the end of __init__
self.show()
def show(self):
self.master.update()
self.master.deiconify()
# Create other window and withdraw self on button click
def other_window(self):
OtherGUI(self.master)
self.master.withdraw()
class OtherGUI:
def __init__(self, master):
# Function for returning to main window, calls MainGUI class
# to create window and withdraws self.
def main_window():
MainGUI(self.master)
self.master.withdraw()
master = self.master = Toplevel(master)
#....Create and .grid() all GUI Widgets....
# Button for switching back to main window
button = Button(text="CLICKME", command=lambda: self.main_window())
使用 MainGUI 中的打印功能,我能够看到,当尝试切换回主窗口时,实际上调用了 show(),并且似乎确实进入了整个类。
这让我感到困惑,因为我只是从其他论坛帖子中真正学会了如何做到这一点,并且使用 root.update() 和 .deiconify() 似乎是大多数人的解决方案,但是我不知道为什么这不是在职的。
有没有人知道我在这里哪里出错了?
由于多种原因,您提供的示例将不起作用。
#really you should build your gui as an inherited class as it makes things much easier to manage in tkinter.
class MainGUI:
def __init__(self, master):
self.master = master
button = Button(text="CLICKME", command=lambda: self.other_window())
# no need for lambda expressions here.
# missing geometry layout... grid(), pack() or place()
self.show()
# self.show does nothing here because your show method is improperly indented.
# your other_window method is also not properly indented.
def show(self):
self.master.update()
self.master.deiconify()
def other_window(self):
OtherGUI(self.master)
self.master.withdraw()
class OtherGUI:
def __init__(self, master):
# this function should be its own method.
def main_window():
MainGUI(self.master)
self.master.withdraw()
master = self.master = Toplevel(master)
# this is not how you should be defining master.
button = Button(text="CLICKME", command=lambda: self.main_window())
# missing geometry layout... grid(), pack() or place()
# your button command is using a lambda to call a class method but your define it as a function instead.
这是您尝试的更简单的版本:
import tkinter as tk
class MainGUI(tk.Tk):
def __init__(self):
super().__init__()
tk.Button(self, text="Open Toplevel", command=self.open_toplevel_window).pack()
def open_toplevel_window(self):
OtherGUI(self)
self.withdraw()
class OtherGUI(tk.Toplevel):
def __init__(self, master):
super().__init__()
tk.Button(self, text="Close top and deiconify main", command=self.main_window).pack()
def main_window(self):
self.master.deiconify()
self.destroy()
MainGUI().mainloop()
正如您在此处看到的,当您从控制主窗口和顶级窗口的 tkinter 类继承时,管理它们变得更容易,并且执行任务的代码更少。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句