我正在使用python 3.7.2和tkinter做一些“ Supplementshop”。我想使用“返回主窗口”按钮。因为我正在通过按钮和功能创建新窗口,所以我认为我无法使用.destroy
方法...经过一整天的测试某些代码后,我仍然没有设法获得此按钮。我想,我现在需要一些帮助。 :D
因为代码已经有大约600行,所以我只是从中做了一个简短的版本。希望足够了。代码:
from tkinter import *
import sys, os
class MainWindow():
def __init__(self, master, title, size):
self.master = master
self.title = title
self.size = size
self.master.title(self.title)
self.master.geometry(self.size)
self.hitext = Label(self.master,
text="some random text,\n to change the previous text lol").pack(fill=X, pady=10)
self.productButton = Button(self.master,
text="second window",
width=15,
command=self.productButtonClicked).place(x=15, y=55)
self.quitMainWindow = Button(self.master,
text="exit",
width=15,
command=mainWindow.quit).place(x=170, y=55)
def productButtonClicked(self):
productWindow = Toplevel()
productFenster = ProductMenuWindow(productWindow, "second window", "260x100")
class ProductMenuWindow():
def __init__(self, master, title, size):
self.master = master
self.title = title
self.size = size
self.master.geometry(self.size)
self.master.title(self.title)
self.text = Label(self.master, text="what do you want to buy?").pack(fill=X, pady=10)
self.gobackButton = Button(self.master,
text="go back to main window",
width=20,
command="").place(x=55, y=50) #here should be the command for the button
if __name__ == "__main__":
mainWindow = Tk()
mainFenster = MainWindow(mainWindow, "root/main/first window", "300x95")
mainWindow.mainloop()
如果我放command=mainWindow.quit
,则主窗口当然会被破坏,程序会停止。因此,这很重要,我现在不进一步介绍了,因为.destroy
在这里不起作用...对于某些英语错误:P
答案 0 :(得分:0)
我做了一些更改。
将我的版本与您的版本进行比较。
from tkinter import *
import sys, os
class MainWindow():
def __init__(self, master, title, size):
self.master = master
self.title = title
self.size = size
self.master.title(self.title)
self.master.geometry(self.size)
self.hitext = Label(self.master,
text="some random text,\n to change the previous text lol").pack(fill=X, pady=10)
self.productButton = Button(self.master,
text="second window",
width=15,
command=self.productButtonClicked).place(x=15, y=55)
self.quitMainWindow = Button(self.master,
text="exit",
width=15,
command=self.on_cancel).place(x=170, y=55)
def productButtonClicked(self):
#productWindow = Toplevel()
obj = ProductMenuWindow(self, "second window", "260x100")
#productFenster = ProductMenuWindow(productWindow,)
def on_cancel(self):
self.master.destroy()
class ProductMenuWindow(Toplevel):
def __init__(self, parent, title, size):
super().__init__(name='product_main_menu')
self.parent = parent
self.title(title)
self.size = size
self.geometry(size)
self.text = Label(self, text="what do you want to buy?").pack(fill=X, pady=10)
self.gobackButton = Button(self,
text="go back to main window",
width=20,
command=self.on_cancel).place(x=55, y=50) #here should be the command for the button
def on_cancel(self):
self.destroy()
if __name__ == "__main__":
mainWindow = Tk()
mainFenster = MainWindow(mainWindow, "root/main/first window", "300x95")
mainWindow.mainloop()
p.s。
super().__init__(name='product_main_menu')
过去,您的ProductMenuWindow
上有单身人士。
如果您写super().__init__()
,您会看到可以打开多个窗口。