python tkinter多个软件界面切换功能实现
小程序的实现采用python中的tkinter创建主窗口,切换窗口采用tkinter模块的的frame窗口控件进行创建,通过不同的按键调用显示不同的操作界面。操作界面的显示使用try…except…finally进行显示。思路:在tkinter中想要实现多个界面的切换,需要创建多个frame,而当frame01切换至frame02或frame03时,需要将当前的显示界面进行关闭,再显示其他的窗口。假设在程序编写过程中,程序主要在界面frame01中进行实现,没有在界面frame02、frame03中进行实现,按钮控件调用frame02 、frame03时软件就会报错,因此程序中采用try…except来处理程序运行异常。因此开始程序书写如下:
try:
frame1.destroy()
frame2.destroy()
frame3.destroy()
except:
pass
但是在此程序中,如果frame01不存在,程序直行道frame01,程序即运行停止,不会执行后面的程序。因此在程序中采用try…except…finally来执行程序,这样不管前面的是否报错,最后都会执行finally后面的代码。
import tkinter
from tkinter import *
from ttkbootstrap import *
class MainWindows(Tk):
def __init__(self):
super().__init__()
self.title("界面切换测试程序") # 给界面添加一个标题
self.geometry("544x344+400+200") # 定义界面尺寸
# self.resizable(0, 0) # 定义界面窗口大小不可改变
# 调用常用变量
self.setup_main_gui()
def setup_main_gui(self):
# 给界面设置一个主题
self.style = Style(theme="flatly")
# 创建一个界面标题
self.label_title = Label(self, text="多界面切换测试程序", font="微软雅黑 20 bold", relief="groove")
self.label_title.place(relwidth=1, relheight=0.18, relx=0, rely=0)
# 创建左侧按钮显示区域
self.Pane_left = PanedWindow(self, relief="groove")
self.Pane_left.place(relwidth=0.15, relheight=0.82, relx=0, rely=0.18)
# 创建界面01控制按钮
self.button_frame01 = Button(self.Pane_left, text="界面一", font="微软雅黑 12 bold", command=self.createframe01)
self.button_frame01.place(relwidth=1, relheight=0.08, relx=0, rely=0.2)
# 创建界面02控制按钮
self.button_frame02 = Button(self.Pane_left, text="界面二", font="微软雅黑 12 bold", command=self.createframe02)
self.button_frame02.place(relwidth=1, relheight=0.08, relx=0, rely=0.5)
# 创建界面03控制按钮
self.button_frame03 = Button(self.Pane_left, text="界面三", font="微软雅黑 12 bold", command=self.createframe03)
self.button_frame03.place(relwidth=1, relheight=0.08, relx=0, rely=0.8)
def setup_frame01(self):
self.frame01 = Frame(self, relief="groove")
self.frame01.place(relwidth=0.84, relheight=0.82, relx=0.16, rely=0.18)
self.label_01 = Label(self.frame01, text="界面显示一", font="微软雅黑 12 bold", bg="green")
self.label_01.place(relwidth=1, relheight=1, relx=0, rely=0)
def setup_frame02(self):
self.frame02 = Frame(self, relief="groove")
self.frame02.place(relwidth=0.84, relheight=0.82, relx=0.16, rely=0.18)
self.label_02 = Label(self.frame02, text="界面显示二", font="微软雅黑 12 bold", bg="red")
self.label_02.place(relwidth=1, relheight=1, relx=0, rely=0)
def setup_frame03(self):
self.frame03 = Frame(self, relief="groove")
self.frame03.place(relwidth=0.84, relheight=0.82, relx=0.16, rely=0.18)
self.label_03 = Label(self.frame03, text="界面显示三", font="微软雅黑 12 bold", bg="blue")
self.label_03.place(relwidth=1, relheight=1, relx=0, rely=0)
def createframe01(self):
try:
self.frame01.destroy()
except:
pass
finally:
try:
self.frame02.destroy()
except:
pass
finally:
try:
self.frame03.destroy()
except:
pass
finally:
self.setup_frame01()
def createframe02(self):
try:
self.frame01.destroy()
except:
pass
finally:
try:
self.frame02.destroy()
except:
pass
finally:
try:
self.frame03.destroy()
except:
pass
finally:
self.setup_frame02()
def createframe03(self):
try:
self.frame01.destroy()
except:
pass
finally:
try:
self.frame02.destroy()
except:
pass
finally:
try:
self.frame03.destroy()
except:
pass
finally:
self.setup_frame03()
if __name__ == "__main__":
windows = MainWindows()
windows.mainloop()