Example code of Python Tkinter for interface switching

Time:2020-2-26

The realization of jump

The main program is equivalent to a table:


 import tkinter as tk
 root = tk.Tk() 

Different frames are equivalent to different tablecloths:


 face1 = tk.Frame(root)
 face2 = tk.Frame(root)
 ...

Each interface uses the method of class to define its own controls and functions. Each interface is built on a custom frame. When realizing the effect of jump interface,

You only need to call Tkinter. Destroy() method to destroy the old interface and generate the objects of the new interface at the same time.

To change the background color and size in the process of switching, you can change the properties of frame control or root (table).

Specific code

# -*- coding: utf-8 -*-
"""
Created on Tue Oct 16 11:15:01 2018
Description: Tkinter interface switch
Version:
 
@author: HJY
"""
import tkinter as tk
 
 
class basedesk():
 def __init__(self,master):
  self.root = master
  self.root.config()
  self.root.title('Base page')
  self.root.geometry('200x200')
  
  initface(self.root)  
    
class initface():
 def __init__(self,master):
  
  self.master = master
  self.master.config(bg='green')
  #Reference interface initface
  self.initface = tk.Frame(self.master,)
  self.initface.pack()
  btn = tk.Button(self.initface,text='change',command=self.change)
  btn.pack()
  
 def change(self,):  
  self.initface.destroy()
  face1(self.master)  
 
class face1():
 def __init__(self,master):
  self.master = master
  self.master.config(bg='blue')
  self.face1 = tk.Frame(self.master,)
  self.face1.pack()
  btn_back = tk.Button(self.face1,text='face1 back',command=self.back)
  btn_back.pack()
 
 def back(self):
  self.face1.destroy()
  initface(self.master)
  
 
if __name__ == '__main__': 
 root = tk.Tk()
 basedesk(root)
 root.mainloop()

Design sketch:

The above example code of Python Tkinter interface switching is all the content shared by Xiaobian. I hope it can give you a reference, and I hope you can support developepaer more.