前置知识: Python GUI – tkinter
有时我们需要开发一个包含多个弹出对话框的应用程序,也就是所谓的“页面框架”。在这里,我们将一步步介绍如何创建多个 Tkinter 页面框架并将它们链接起来!这段代码可以作为更复杂的 Python GUI 应用程序的模板,例如为虚拟实验室、教室实验等创建交互界面。
让我们来看看具体的步骤:
- 创建三个不同的页面。在这里我们设计了三个页面:作为主页的起始页、页面一(Page 1)和页面二(Page 2)。
- 为每个页面框架创建一个容器。
- 我们定义了四个类。第一个是 INLINECODE3bb12fc0 类,我们在其中初始化了三个框架,并定义了一个 INLINECODEf7ec9a45 函数,每次用户点击按钮时都会调用该函数。
StartPage(起始页)非常简单,包含两个按钮,分别用于跳转到 Page 1 和 Page 2。- Page 1 有两个按钮,一个用于跳转到 Page 2,另一个用于返回 Start Page。
- Page 2 也有两个按钮,一个用于跳转到 Page 1,另一个用于返回 StartPage。
- 这是一个简单的 Tkinter 框架导航应用示例。
- 这可以作为更复杂应用程序的模板,我们可以在此基础上添加多种功能。
应用程序从 StartPage 开始,正如 INLINECODE36a5af3d 类中所示。在 StartApp 中,有两个按钮。点击按钮会带你进入相应的页面。你可以向这些页面添加图像和图表,并添加复杂的功能。这些页面也有两个按钮。每当按下按钮时,就会调用 INLINECODE4ca001b2,从而显示相应的页面。
下面是具体的代码实现:
Python3
`
import tkinter as tk
from tkinter import ttk
LARGEFONT =("Verdana", 35)
class tkinterApp(tk.Tk):
# __init__ function for class tkinterApp
def __init__(self, *args, **kwargs):
# __init__ function for class Tk
tk.Tk.__init__(self, *args, **kwargs)
# creating a container
container = tk.Frame(self)
container.pack(side = "top", fill = "both", expand = True)
container.grid_rowconfigure(0, weight = 1)
container.grid_columnconfigure(0, weight = 1)
# initializing frames to an empty array
self.frames = {}
# iterating through a tuple consisting
# of the different page layouts
for F in (StartPage, Page1, Page2):
frame = F(container, self)
# initializing frame of that object from
# startpage, page1, page2 respectively with
# for loop
self.frames[F] = frame
frame.grid(row = 0, column = 0, sticky ="nsew")
self.show_frame(StartPage)
# to display the current frame passed as
# parameter
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
# first window frame startpage
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
# label of frame Layout 2
label = ttk.Label(self, text ="Startpage", font = LARGEFONT)
# putting the grid in its place by using
# grid
label.grid(row = 0, column = 4, padx = 10, pady = 10)
button1 = ttk.Button(self, text ="Page 1",
command = lambda : controller.show_frame(Page1))
# putting the button in its place by
# using grid
button1.grid(row = 1, column = 1, padx = 10, pady = 10)
## button to show frame 2 with text layout2
button2 = ttk.Button(self, text ="Page 2",
command = lambda : controller.show_frame(Page2))
# putting the button in its place by
# using grid
button2.grid(row = 2, column = 1, padx = 10, pady = 10)
# second window frame page1
class Page1(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = ttk.Label(self, text ="Page 1", font = LARGEFONT)
label.grid(row = 0, column = 4, padx = 10, pady = 10)
# button to show frame 2 with text
# layout2
button1 = ttk.Button(self, text ="StartPage",
command = lambda : controller.show_frame(StartPage))
# putting the button in its place
# by using grid
button1.grid(row = 1, column = 1, padx = 10, pady = 10)
# button to show frame 2 with text
# layout2
button2 = ttk.Button(self, text ="Page 2",
command = lambda : controller.show_frame(Page2))
# putting the button in its place by
# using grid
button2.grid(row = 2, column = 1, padx = 10, pady = 10)
# third window frame page2
class Page2(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = ttk.Label(self, text ="Page 2", font = LARGEFONT)