首页 技术 正文
技术 2022年11月17日
0 收藏 554 点赞 4,364 浏览 2660 个字

知识点:

使用 tkinter.Frame.tkraise() 函数去提升当前 tkinter.Frame 的 z 轴顺序,使得多个 tkinter.Frame 的可见性得以切换

本文基于:win7 + python34

1

tkinter的GUI设计:界面与逻辑分离(三)– 多页面

2

tkinter的GUI设计:界面与逻辑分离(三)– 多页面

3

tkinter的GUI设计:界面与逻辑分离(三)– 多页面

4

tkinter的GUI设计:界面与逻辑分离(三)– 多页面

5

import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figureimport tkinter as tk
from tkinter import ttkLARGE_FONT= ("Verdana", 12)class Application(tk.Tk):
'''
多页面测试程序
界面与逻辑分离
'''
def __init__(self): super().__init__() self.iconbitmap(default="kankan_01.ico")
self.wm_title("多页面测试程序") container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1) self.frames = {}
for F in (StartPage, PageOne, PageTwo, PageThree):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew") # 四个页面的位置都是 grid(row=0, column=0), 位置重叠,只有最上面的可见!! self.show_frame(StartPage) def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise() # 切换,提升当前 tk.Frame z轴顺序(使可见)!!此语句是本程序的点睛之处class StartPage(tk.Frame):
'''主页'''
def __init__(self, parent, root):
super().__init__(parent)
label = tk.Label(self, text="这里是主页", font=LARGE_FONT)
label.pack(pady=10,padx=10) button1 = ttk.Button(self, text="去到第一页", command=lambda: root.show_frame(PageOne)).pack()
button2 = ttk.Button(self, text="去到第二页", command=lambda: root.show_frame(PageTwo)).pack()
button3 = ttk.Button(self, text="去到绘图页", command=lambda: root.show_frame(PageThree)).pack()class PageOne(tk.Frame):
'''第一页'''
def __init__(self, parent, root):
super().__init__(parent)
label = tk.Label(self, text="这是第一页", font=LARGE_FONT)
label.pack(pady=10,padx=10) button1 = ttk.Button(self, text="回到主页", command=lambda: root.show_frame(StartPage)).pack()
button2 = ttk.Button(self, text="去到第二页", command=lambda: root.show_frame(PageTwo)).pack()class PageTwo(tk.Frame):
'''第二页'''
def __init__(self, parent, root):
super().__init__(parent)
label = tk.Label(self, text="这是第二页", font=LARGE_FONT)
label.pack(pady=10,padx=10) button1 = ttk.Button(self, text="回到主页", command=lambda: root.show_frame(StartPage)).pack()
button2 = ttk.Button(self, text="去到第一页", command=lambda: root.show_frame(PageOne)).pack()class PageThree(tk.Frame):
'''第三页'''
def __init__(self, parent, root):
super().__init__(parent)
tk.Label(self, text="这是绘图页", font=LARGE_FONT).pack(pady=10,padx=10) button1 = ttk.Button(self, text="回到主页", command=lambda: root.show_frame(StartPage)).pack() fig = Figure(figsize=(5,5), dpi=100)
a = fig.add_subplot(111)
a.plot([1,2,3,4,5,6,7,8],[5,6,1,3,8,9,3,5]) canvas = FigureCanvasTkAgg(fig, self)
canvas.show()
canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True) toolbar = NavigationToolbar2TkAgg(canvas, self)
toolbar.update()
canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)if __name__ == '__main__':
# 实例化Application
app = Application() # 主消息循环:
app.mainloop()
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,491
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,492
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,293