如果你以前使用过 Tkinter,你可能知道有很多GUI功能可以用来创建一个应用程序。
但是,在创建应用程序时,你很快就会意识到,模块比眼睛更重要。
tkinter
中有很多隐藏的功能,其中一个是模块中使用类的方法。
设置 Tkinter 模块
没有必要安装任何模块,因为tkinter模块是标准Python库的一部分。
然而,本文将处理tkinter
模块的稍微先进的形式,因此,建议您通过初学者系列。
因此,在你继续前进之前,不要忘了在这里阅读TKinter的基本教程:
如果你完成了基本教程,让我们开始使用tkinter
模块。
为了与类(/community/tutorials/python-classes-objects)工作,我们需要导入tkinter
模块。
1# Importing the tkinter module
2import tkinter as tk
3
4# Used for styling the GUI
5from tkinter import tkk
我们还将单独导入ttk
包,以便方便使用。
在 Tkinter 中与班级合作
让我们来了解如何在Tkinter中与类一起工作。应用程序的工作方式非常简单。
我们首先创建一个 _root 窗口,在上面我们放置一个 single frame。
为了使它看起来像一个具有不同的窗口的应用程序,我们还将创建一个函数,该函数将从一个框架切换到另一个。
这给用户一个幻想,他们正在被重定向到不同的窗口/标签,但,实际上,只是在框架之间切换。
我们将使用的框架是主页
,侧面页
和完成屏幕
。
我们将用来切换它们之间的方法是show_frame()
方法。
使用代码
让我们创建一个基础类,从那里我们将访问所有其他类 / 框架开始。
1# Allowing us to extend from the Tk class
2class testClass(tk.Tk):
从 tk.Tk
类扩展,使我们能够与 Tk()
类中存在的组件一起工作。
1、初始化班级
为了初始化类,我们使用__init__
函数,这会创建一个在我们从类中形成对象时自动运行的方法。
以类似的方式,我们也通过 tk.Tk
init 来初始化类。
1import tkinter as tk
2from tkinter import ttk
3
4class windows(tk.Tk):
5 def __init__(self, *args, **kwargs):
6 tk.Tk.__init__(self, *args, **kwargs)
7 # Adding a title to the window
8 self.wm_title("Test Application")
9
10 # creating a frame and assigning it to container
11 container = tk.Frame(self, height=400, width=600)
12 # specifying the region where the frame is packed in root
13 container.pack(side="top", fill="both", expand=True)
14
15 # configuring the location of the container using grid
16 container.grid_rowconfigure(0, weight=1)
17 container.grid_columnconfigure(0, weight=1)
18
19 # We will now create a dictionary of frames
20 self.frames = {}
21 # we'll create the frames themselves later but let's add the components to the dictionary.
22 for F in (MainPage, SidePage, CompletionScreen):
23 frame = F(container, self)
24
25 # the windows class acts as the root window for the frames.
26 self.frames[F] = frame
27 frame.grid(row=0, column=0, sticky="nsew")
28
29 # Using a method to switch frames
30 self.show_frame(MainPage)
创建一个方法来切换视图框架
现在,我们已经创建了__init__
并指定了我们将使用的框架,我们可以创建一种方法来切换我们正在查看的框架。
1def show_frame(self, cont):
2 frame = self.frames[cont]
3 # raises the current frame to the top
4 frame.tkraise()
3、为框架创建多个类
现在,我们创建不同的类,作为框架,使用show_frame()
方法进行切换。
1class MainPage(tk.Frame):
2 def __init__(self, parent, controller):
3 tk.Frame.__init__(self, parent)
4 label = tk.Label(self, text="Main Page")
5 label.pack(padx=10, pady=10)
6
7 # We use the switch_window_button in order to call the show_frame() method as a lambda function
8 switch_window_button = tk.Button(
9 self,
10 text="Go to the Side Page",
11 command=lambda: controller.show_frame(SidePage),
12 )
13 switch_window_button.pack(side="bottom", fill=tk.X)
14
15class SidePage(tk.Frame):
16 def __init__(self, parent, controller):
17 tk.Frame.__init__(self, parent)
18 label = tk.Label(self, text="This is the Side Page")
19 label.pack(padx=10, pady=10)
20
21 switch_window_button = tk.Button(
22 self,
23 text="Go to the Completion Screen",
24 command=lambda: controller.show_frame(CompletionScreen),
25 )
26 switch_window_button.pack(side="bottom", fill=tk.X)
27
28class CompletionScreen(tk.Frame):
29 def __init__(self, parent, controller):
30 tk.Frame.__init__(self, parent)
31 label = tk.Label(self, text="Completion Screen, we did it!")
32 label.pack(padx=10, pady=10)
33 switch_window_button = ttk.Button(
34 self, text="Return to menu", command=lambda: controller.show_frame(MainPage)
35 )
36 switch_window_button.pack(side="bottom", fill=tk.X)
如果您注意到,这些类被添加到主类中,使用self.frames
变量。
我会把执行类的命令放在一个__name__=="main
中,但你也可以直接使用它。
1if __name__ == "__main__":
2 testObj = windows()
3 testObj.mainloop()
向前移动
我们现在已经完成了定义类和方法,并且可以运行这个脚本。
这应该为您提供一个小窗口,允许您通过按钮切换框架之间。
在更高层次上,您甚至可以在应用程序顶部的菜单栏中切换框架之间的功能。
结论
在tkinter
模块中与类一起工作,为工作和创建新应用打开了许多大门。
我们今天已经在相当多的代码上工作了,所以我们在同一页面上,这里是(https://gist.github.com/dat-adi/baf5a4a64358a5f1d13452ef7b584cbc)!
如果您想查看一个实时示例,请查看我的项目 Eisen的门票,该项目实现了包括课程在内的许多Tkinter功能,并使用了 SQLite3。