MainWindow界面和SubWindow界面间进行切换,点击MainWindow的windowlist菜单下的SubWindow子菜单,
跳转到SubWindow界面,点击SubWindow界面下的返回按钮,跳转到MainWindow界面。
使用Python的Tkinter包
代码如下:
class StartApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self._frame = None
self.switch_frame(MainWindow)
def switch_frame(self, frame_class):
new_frame = frame_class(self)
if self._frame is not None:
self._frame.destroy()
self._frame = new_frame
self._frame.pack()
self._frame.tkraise()
#菜单栏设置
menubar = self._frame.menubar(self)
self.configure(menu=menubar)
if __name__ == '__main__':
app = StartApp()
app.mainloop()
2 .MainWindow类
class MainWindow(tk.Frame):
def __init__(self,master=None):
super().__init__(master)
self.master=master
self.master.title('FirstPage')
self.create_widgets()
def menubar(self, root):
menubar = tk.Menu(root)
pagelistMenu= tk.Menu(menubar)
pagelistMenu.add_command(label="SubWindow",
command=lambda: self.master.switch_frame(SubWindow),underline=0)
menubar.add_cascade(label="WindowList", menu=pagelistMenu)
return menubar
def create_widgets(self):
self.labelframeleft = tk.LabelFrame(self, text="入力画像",width=200,height=200)
self.labelframeleft.pack(padx=2,side=tk.LEFT, fill="both", expand="yes")
#---画像表示-----tk.PhotoImage函数的''wei图像数据
image_data = tk.PhotoImage('',master=self.labelframeleft)
self.imgLabel = tk.Label(self.labelframeleft,image=image_data)
self.imgLabel.pack(side="top")
3.SubWindow类
class SubWindow(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
self.master=master
tk.Frame.configure(self,bg='red')
tk.Label(self, text="SubWindow", font=('Helvetica', 18, "bold")).pack(side="top", fill="x", pady=5)
tk.Button(self, text="返回",
command=lambda: master.switch_frame(MainWindow)).pack()
def menubar(self, root):
menubar = tk.Menu(root)
pageMenu = tk.Menu(menubar)
pageMenu.add_command(label="Window")
menubar.add_cascade(label="Page", menu=pageMenu)
return menubar