web123456

tkinter window content switching example

  • import tkinter as tk
  • class Mainfrm():
  • '''Put red and blue'''
  • def __init__(self,parent=None):
  • super().__init__(parent)
  • (bg='green')
  • (expand=1,fill='both')
  • class Redfrm():
  • def __init__(self,parent=None):
  • super().__init__(parent)
  • (bg='red',width=50,height=40)
  • (expand=1,fill='both',side='top')
  • class Blufrm():
  • def __init__(self,parent=None):
  • super().__init__(parent)
  • (bg='blue',width=50,height=40)
  • (expand=1,fill='both',side='top')
  • class Mainmenu():
  • def __init__(self,parent=None,frame=None):
  • super().__init__(parent)
  • = frame
  • self.add_command(label='red',command=)
  • self.add_command(label='blue',command=)
  • (menu=self)
  • def tored(self):
  • children = .winfo_children()
  • for c in children:
  • ()
  • redfrm = Redfrm()
  • def toblue(self):
  • children = .winfo_children()
  • for c in children:
  • ()
  • redfrm = Blufrm()
  • if __name__ == "__main__":
  • root = ()
  • ("Interface Demo")
  • ('300x500+10+10')
  • mainfrm = Mainfrm(root)
  • mmenu = Mainmenu(root,frame=mainfrm)
  • ()