Tkinter是一个Python自带的GUI框架,虽然现在主流的还是用pyqt的多一些,但是Tkinter在环境配置上可以节省很多工作,可以用来做一些小项目。如果是大型项目,用pyqt或者QT确实会更加专业一些。本文主要介绍一些简单的Tkinter的示例,比如文本框定义、标签定义和TreeView定义等。
最基本的来说,类似于代码界的Hello Word,我们可以用Tkinter创建一个简单的窗体:
import tkinter as tk
# 主窗口
root = tk.Tk()
root.title('Title')
root.geometry('320x240')
# 启动运行
root.mainloop()
一般我们窗体上会有一个菜单栏,常用的菜单功能比如打开文件、文本操作等:
import tkinter as tk
root = tk.Tk()
root.title('Title')
root.geometry('320x240')
# 菜单
menubar = tk.Menu(root)
# 子菜单
submenu = tk.Menu(menubar, activebackground='blue')
submenu.add_command(label='Sub Menu 1', command=None)
submenu.add_command(label='Sub Menu 2', command=None)
# 把子菜单添加到主菜单中
menubar.add_cascade(label='Menu 1', menu=submenu)
menubar.add_command(label='Quit', command=root.destroy)
# 把主菜单配置到窗体中
root.config(menu=menubar)
root.mainloop()
import tkinter as tk
root = tk.Tk()
root.title('Title')
root.geometry('320x240')
menubar = tk.Menu(root)
submenu = tk.Menu(menubar, activebackground='blue')
submenu.add_command(label='Sub Menu 1', command=None)
submenu.add_command(label='Sub Menu 2', command=None)
menubar.add_cascade(label='Menu 1', menu=submenu)
menubar.add_command(label='Quit', command=root.destroy)
# 创建文本框,只能用字符数设置文本框的宽度
text_box = tk.Entry(root, bd=10)
# 设置默认文本内容
text_box.insert(0, 'Default Text')
# 占满当前布局
text_box.pack()
root.config(menu=menubar)
root.mainloop()
除了Entry文本框,还可以使用Text来定义文本框。如果使用Text定义文本框,定义时可以配置大小。如果对比这两个控件,最简单的来说就是,Entry适用于单行的输入(如登录界面的账号密码等),Text适用于多行的文本输入(文本编辑器)。
import tkinter as tk
root = tk.Tk()
root.title('Title')
root.geometry('320x240')
menubar = tk.Menu(root)
submenu = tk.Menu(menubar, activebackground='blue')
submenu.add_command(label='Sub Menu 1', command=None)
submenu.add_command(label='Sub Menu 2', command=None)
menubar.add_cascade(label='Menu 1', menu=submenu)
menubar.add_command(label='Quit', command=root.destroy)
# 设置文本框的大小
text_box = tk.Text(root, height=10, width=20)
text_box.insert('0.0', 'Default Text')
text_box.pack()
root.config(menu=menubar)
root.mainloop()
通俗的来看,树的结构就跟本地存放文件的目录一样,分有层级。如果广义的来看,凡是有索引的键值对结构,甚至是普通的矩阵形式,都可以用这种目录树的形式来显示。
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.title('Title')
root.geometry('320x240')
menubar = tk.Menu(root)
submenu = tk.Menu(menubar, activebackground='blue')
submenu.add_command(label='Sub Menu 1', command=None)
submenu.add_command(label='Sub Menu 2', command=None)
menubar.add_cascade(label='Menu 1', menu=submenu)
menubar.add_command(label='Quit', command=root.destroy)
text_box = tk.Text(root, height=10, width=20)
text_box.insert('0.0', 'Default Text')
text_box.pack()
# 定义树结构
tree = ttk.Treeview(root)
# 一级节点
tree0 = tree.insert("", 0, "Tree-0", text="Tree-0", values=("0"))
tree1 = tree.insert("", 1, "Tree-1", text="Tree-1", values=("1"))
# 二级节点
tree00 = tree.insert(tree0, 0, "Tree-0-0", text="Tree-0-0", values=("0-0"))
tree01 = tree.insert(tree0, 1, "Tree-0-1", text="Tree-0-1", values=("0-1"))
tree02 = tree.insert(tree0, 2, "Tree-0-2", text="Tree-0-2", values=("0-2"))
# 默认布局
tree.pack()
root.config(menu=menubar)
root.mainloop()
上一个章节中的默认布局是上下布局,我们可以手动设定一个横向的grid布局,行和列分别用row和column来设定:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.title('Title')
root.geometry('320x240')
menubar = tk.Menu(root)
submenu = tk.Menu(menubar, activebackground='blue')
submenu.add_command(label='Sub Menu 1', command=None)
submenu.add_command(label='Sub Menu 2', command=None)
menubar.add_cascade(label='Menu 1', menu=submenu)
menubar.add_command(label='Quit', command=root.destroy)
text_box = tk.Text(root, height=10, width=20)
text_box.insert('0.0', 'Default Text')
# 文本框放在第一行第一列
text_box.grid(row=0, column=0)
tree = ttk.Treeview(root)
tree0 = tree.insert("", 0, "Tree-0", text="Tree-0", values=("0"))
tree1 = tree.insert("", 1, "Tree-1", text="Tree-1", values=("1"))
tree00 = tree.insert(tree0, 0, "Tree-0-0", text="Tree-0-0", values=("0-0"))
tree01 = tree.insert(tree0, 1, "Tree-0-1", text="Tree-0-1", values=("0-1"))
tree02 = tree.insert(tree0, 2, "Tree-0-2", text="Tree-0-2", values=("0-2"))
# 树形图放在第一行第二列
tree.grid(row=0, column=1)
root.config(menu=menubar)
root.mainloop()
需要注意的是,pack和grid两者是冲突的,不能同时使用。
按钮Button是一个用于事件触发的组件,定义形式较为简单:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.title('Title')
root.geometry('320x240')
menubar = tk.Menu(root)
submenu = tk.Menu(menubar, activebackground='blue')
submenu.add_command(label='Sub Menu 1', command=None)
submenu.add_command(label='Sub Menu 2', command=None)
menubar.add_cascade(label='Menu 1', menu=submenu)
menubar.add_command(label='Quit', command=root.destroy)
text_box = tk.Text(root, height=10, width=20)
text_box.insert('0.0', 'Default Text')
text_box.grid(row=0, column=0)
# 定义按钮
button = tk.Button(root, text='Button', command=None)
# 把按钮放在第一行第二列
button.grid(row=0, column=1)
tree = ttk.Treeview(root)
tree0 = tree.insert("", 0, "Tree-0", text="Tree-0", values=("0"))
tree1 = tree.insert("", 1, "Tree-1", text="Tree-1", values=("1"))
tree00 = tree.insert(tree0, 0, "Tree-0-0", text="Tree-0-0", values=("0-0"))
tree01 = tree.insert(tree0, 1, "Tree-0-1", text="Tree-0-1", values=("0-1"))
tree02 = tree.insert(tree0, 2, "Tree-0-2", text="Tree-0-2", values=("0-2"))
# 树形结构放在第一行第三列
tree.grid(row=0, column=2)
root.config(menu=menubar)
root.mainloop()
虽然滚动条是一个很常见的功能,但是如果我们要在网格布局里面加滚动条,那就要把那些需要加滚动条的控件单独放到某个容器内,常用的有Widget和Frame。Frame是从Widget继承过来的,可以加一些边框阴影什么的,这里我们先用Frame来做一个简单示例:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.title('Title')
root.geometry('320x240')
menubar = tk.Menu(root)
submenu = tk.Menu(menubar, activebackground='blue')
submenu.add_command(label='Sub Menu 1', command=None)
submenu.add_command(label='Sub Menu 2', command=None)
menubar.add_cascade(label='Menu 1', menu=submenu)
menubar.add_command(label='Quit', command=root.destroy)
# 定义一个指定大小的Frame
left_frame = tk.Frame(root, height=10, width=20)
text_box = tk.Text(left_frame, height=10, width=20)
text_box.insert('0.0', 'Default Text')
# 定义滚动条
scroll_text = tk.Scrollbar(left_frame)
# 定义滚动条的滚动方向
scroll_text.pack(side=tk.RIGHT, fill=tk.Y)
# 耦合滚动条与控件的视图
scroll_text.config(command=text_box.yview)
# 把滚动条添加到文本框的操作内
text_box.config(yscrollcommand=scroll_text.set)
# 这里是一个关键点,滚动条不能与grid共用,因此这里需要创建一个独立的容器才能加上滚动条
text_box.pack()
# 把Frame放在第一行第一列,这里放的就不是文本框控件了
left_frame.grid(row=0, column=0)
button = tk.Button(root, text='Button', command=None)
button.grid(row=0, column=1)
tree = ttk.Treeview(root)
tree0 = tree.insert("", 0, "Tree-0", text="Tree-0", values=("0"))
tree1 = tree.insert("", 1, "Tree-1", text="Tree-1", values=("1"))
tree00 = tree.insert(tree0, 0, "Tree-0-0", text="Tree-0-0", values=("0-0"))
tree01 = tree.insert(tree0, 1, "Tree-0-1", text="Tree-0-1", values=("0-1"))
tree02 = tree.insert(tree0, 2, "Tree-0-2", text="Tree-0-2", values=("0-2"))
tree.grid(row=0, column=2)
root.config(menu=menubar)
root.mainloop()
真正要做项目的时候,还是需要一个对象封装,便于局部的控制和更新,也方便功能维护与测试。一个应用对象应该包含GUI界面和操作函数,我们可以先对上面的这个简单案例做一个模块分离,构建一个简单的应用类型:
import tkinter as tk
from tkinter import ttk
# 自定义一个应用的对象
class Object:
def __init__(self):
# GUI界面只是应用的一个内置属性
self.root = tk.Tk()
self.root.title('Title')
# 分模块初始化
self.init_menu()
self.init_text_box()
self.init_button()
self.init_tree()
def init_menu(self):
“”“菜单栏模块封装”“”
menubar = tk.Menu(self.root)
submenu = tk.Menu(menubar, activebackground='blue')
submenu.add_command(label='Sub Menu 1', command=None)
submenu.add_command(label='Sub Menu 2', command=None)
menubar.add_cascade(label='Menu 1', menu=submenu)
menubar.add_command(label='Quit', command=self.root.destroy)
self.root.config(menu=menubar)
def init_text_box(self):
“”“文本框模块封装”“”
left_frame = tk.Frame(self.root)
text_box = tk.Text(left_frame, height=10, width=20)
text_box.insert('0.0', 'Default Text')
scroll_text = tk.Scrollbar(left_frame)
scroll_text.pack(side=tk.RIGHT, fill=tk.Y)
scroll_text.config(command=text_box.yview)
text_box.config(yscrollcommand=scroll_text.set)
text_box.pack()
left_frame.grid(row=0, column=0)
def init_button(self):
“”“按钮模块封装”“”
button = tk.Button(self.root, text='Button', command=None)
button.grid(row=0, column=1)
def init_tree(self):
“”“树形结构模块封装”“”
right_frame = tk.Frame(self.root)
tree = ttk.Treeview(right_frame)
tree0 = tree.insert("", 0, "Tree-0", text="Tree-0", values=("0"))
tree1 = tree.insert("", 1, "Tree-1", text="Tree-1", values=("1"))
tree00 = tree.insert(tree0, 0, "Tree-0-0", text="Tree-0-0", values=("0-0"))
tree01 = tree.insert(tree0, 1, "Tree-0-1", text="Tree-0-1", values=("0-1"))
tree02 = tree.insert(tree0, 2, "Tree-0-2", text="Tree-0-2", values=("0-2"))
tree10 = tree.insert(tree1, 0, "Tree-1-0", text="Tree-1-0", values=("1-0"))
tree100 = tree.insert(tree10, 0, "Tree-1-0-0", text="Tree-1-0-0\ntest\nmulti-line", values=("1-0-0"))
tree.pack()
right_frame.grid(row=0, column=2)
def run(self):
“”“运行启动”“”
self.root.mainloop()
if __name__ == '__main__':
my_app = Object()
my_app.run()
在此基础之上我们可以实现一些功能函数,比如给按钮添加一些功能:
import tkinter as tk
from tkinter import ttk
class Object:
def __init__(self):
self.root = tk.Tk()
self.root.title('Title')
self.init_menu()
self.init_text_box()
self.init_button()
self.init_tree()
def init_menu(self):
menubar = tk.Menu(self.root)
submenu = tk.Menu(menubar, activebackground='blue')
submenu.add_command(label='Sub Menu 1', command=None)
submenu.add_command(label='Sub Menu 2', command=None)
menubar.add_cascade(label='Menu 1', menu=submenu)
menubar.add_command(label='Quit', command=self.root.destroy)
self.root.config(menu=menubar)
def init_text_box(self):
left_frame = tk.Frame(self.root)
text_box = tk.Text(left_frame, height=10, width=20)
text_box.insert('0.0', 'Default Text')
scroll_text = tk.Scrollbar(left_frame)
scroll_text.pack(side=tk.RIGHT, fill=tk.Y)
scroll_text.config(command=text_box.yview)
text_box.config(yscrollcommand=scroll_text.set)
text_box.pack()
left_frame.grid(row=0, column=0)
def init_button(self):
# 给按钮控件链接了一个update_tree的内部函数
button = tk.Button(self.root, text='Button', command=self.update_tree)
button.grid(row=0, column=1)
def init_tree(self):
right_frame = tk.Frame(self.root)
tree = ttk.Treeview(right_frame)
tree0 = tree.insert("", 0, "Tree-0", text="Tree-0", values=("0"))
tree1 = tree.insert("", 1, "Tree-1", text="Tree-1", values=("1"))
tree00 = tree.insert(tree0, 0, "Tree-0-0", text="Tree-0-0", values=("0-0"))
tree01 = tree.insert(tree0, 1, "Tree-0-1", text="Tree-0-1", values=("0-1"))
tree02 = tree.insert(tree0, 2, "Tree-0-2", text="Tree-0-2", values=("0-2"))
tree10 = tree.insert(tree1, 0, "Tree-1-0", text="Tree-1-0", values=("1-0"))
tree100 = tree.insert(tree10, 0, "Tree-1-0-0", text="Tree-1-0-0\ntest\nmulti-line", values=("1-0-0"))
tree.pack()
right_frame.grid(row=0, column=2)
def update_tree(self):
““”该函数的主要功能,是替换树形结构的内容“””
right_frame = tk.Frame(self.root)
tree = ttk.Treeview(right_frame)
tree0 = tree.insert("", 0, "Tree-0", text="Tree-0", values=("0"))
tree1 = tree.insert("", 1, "Tree-1", text="Tree-1", values=("1"))
tree.pack()
right_frame.grid(row=0, column=2)
def run(self):
self.root.mainloop()
if __name__ == '__main__':
my_app = Object()
my_app.run()
点击按钮之前的树形结构显示:
点击按钮之后的树形结构显示:
标签也比较容易理解,通常就是一些界面上不可变更的文字内容,用于标明各个控件的功能。
import tkinter as tk
from tkinter import ttk
class Object:
def __init__(self):
self.root = tk.Tk()
self.root.title('Title')
self.init_menu()
self.init_text_box()
self.init_button()
self.init_tree()
init_text = "Welcome to tkinter."
# 用一个文本参数来初始化底部标签控件,类似于提示信息或者日志信息的内容显示
self.init_label(init_text)
def init_menu(self):
menubar = tk.Menu(self.root)
submenu = tk.Menu(menubar, activebackground='blue')
submenu.add_command(label='Sub Menu 1', command=None)
submenu.add_command(label='Sub Menu 2', command=None)
menubar.add_cascade(label='Menu 1', menu=submenu)
menubar.add_command(label='Quit', command=self.root.destroy)
self.root.config(menu=menubar)
def init_text_box(self):
left_frame = tk.Frame(self.root)
text_box = tk.Text(left_frame, height=10, width=20)
text_box.insert('0.0', 'Default Text')
scroll_text = tk.Scrollbar(left_frame)
scroll_text.pack(side=tk.RIGHT, fill=tk.Y)
scroll_text.config(command=text_box.yview)
text_box.config(yscrollcommand=scroll_text.set)
text_box.pack()
left_frame.grid(row=0, column=0)
def init_button(self):
button = tk.Button(self.root, text='Button', command=self.update_tree)
button.grid(row=0, column=1)
def init_tree(self):
right_frame = tk.Frame(self.root)
tree = ttk.Treeview(right_frame)
tree0 = tree.insert("", 0, "Tree-0", text="Tree-0", values=("0"))
tree1 = tree.insert("", 1, "Tree-1", text="Tree-1", values=("1"))
tree00 = tree.insert(tree0, 0, "Tree-0-0", text="Tree-0-0", values=("0-0"))
tree01 = tree.insert(tree0, 1, "Tree-0-1", text="Tree-0-1", values=("0-1"))
tree02 = tree.insert(tree0, 2, "Tree-0-2", text="Tree-0-2", values=("0-2"))
tree10 = tree.insert(tree1, 0, "Tree-1-0", text="Tree-1-0", values=("1-0"))
tree100 = tree.insert(tree10, 0, "Tree-1-0-0", text="Tree-1-0-0\ntest\nmulti-line", values=("1-0-0"))
tree.pack()
right_frame.grid(row=0, column=2)
def init_label(self, text):
“”“添加标签控件,放在第二行”“”
information = tk.Label(self.root, text=text)
information.grid(row=1)
def update_tree(self):
right_frame = tk.Frame(self.root)
tree = ttk.Treeview(right_frame)
tree0 = tree.insert("", 0, "Tree-0", text="Tree-0", values=("0"))
tree1 = tree.insert("", 1, "Tree-1", text="Tree-1", values=("1"))
tree.pack()
right_frame.grid(row=0, column=2)
def run(self):
self.root.mainloop()
if __name__ == '__main__':
my_app = Object()
my_app.run()
用标签这个控件,还可以结合可变的交互式输入内容,例如我们用到的文本框里面的文字,做一个信息提示小组件
import tkinter as tk
from tkinter import ttk
class Object:
def __init__(self):
self.root = tk.Tk()
self.root.title('Title')
self.init_menu()
# 初始化文本框对象
self.text_box = self.init_text_box()
self.init_button()
self.init_tree()
init_text = "Welcome to tkinter."
# 初始化标签对象
self.label=self.init_label(init_text)
def init_menu(self):
menubar = tk.Menu(self.root)
submenu = tk.Menu(menubar, activebackground='blue')
submenu.add_command(label='Sub Menu 1', command=None)
submenu.add_command(label='Sub Menu 2', command=None)
menubar.add_cascade(label='Menu 1', menu=submenu)
menubar.add_command(label='Quit', command=self.root.destroy)
self.root.config(menu=menubar)
def init_text_box(self):
left_frame = tk.Frame(self.root)
text_box = tk.Text(left_frame, height=10, width=20)
text_box.insert('0.0', 'Default Text')
scroll_text = tk.Scrollbar(left_frame)
scroll_text.pack(side=tk.RIGHT, fill=tk.Y)
scroll_text.config(command=text_box.yview)
text_box.config(yscrollcommand=scroll_text.set)
text_box.pack()
left_frame.grid(row=0, column=0)
# 返回文本框对象
return text_box
def init_button(self):
button = tk.Button(self.root, text='Button', command=self.update_tree)
button.grid(row=0, column=1)
def init_tree(self):
right_frame = tk.Frame(self.root)
tree = ttk.Treeview(right_frame)
tree0 = tree.insert("", 0, "Tree-0", text="Tree-0", values=("0"))
tree1 = tree.insert("", 1, "Tree-1", text="Tree-1", values=("1"))
tree00 = tree.insert(tree0, 0, "Tree-0-0", text="Tree-0-0", values=("0-0"))
tree01 = tree.insert(tree0, 1, "Tree-0-1", text="Tree-0-1", values=("0-1"))
tree02 = tree.insert(tree0, 2, "Tree-0-2", text="Tree-0-2", values=("0-2"))
tree10 = tree.insert(tree1, 0, "Tree-1-0", text="Tree-1-0", values=("1-0"))
tree100 = tree.insert(tree10, 0, "Tree-1-0-0", text="Tree-1-0-0\ntest\nmulti-line", values=("1-0-0"))
tree.pack()
right_frame.grid(row=0, column=2)
def init_label(self, text):
information = tk.Label(self.root, text=text)
information.grid(row=1)
return information
def update_tree(self):
right_frame = tk.Frame(self.root)
tree = ttk.Treeview(right_frame)
tree0 = tree.insert("", 0, "Tree-0", text="Tree-0", values=("0"))
tree1 = tree.insert("", 1, "Tree-1", text="Tree-1", values=("1"))
tree.pack()
right_frame.grid(row=0, column=2)
# 在按钮的功能函数中增加对标签控件的刷新
self.update_label()
def update_label(self):
text = self.text_box.get("1.0", "end")
# 销毁原本的标签内容
self.label.after(0, self.label.destroy())
# 输入新的标签内容
self.label = self.init_label(text)
def run(self):
self.root.mainloop()
if __name__ == '__main__':
my_app = Object()
my_app.run()
第一次点击按钮
更新文本内容后,第二次点击按钮
本文主要介绍一些Python的Tkinter GUI框架的常用功能模块,包含基本窗口的创建、菜单栏、文本框、TreeView、按钮、滚动条、标签的设定等,另外包含了一些面向对象的GUI的简单示例。总的来说,Tkinter加上第三方的ttk,基本的GUI功能是都具备的,可以用来实现一些简单的小项目。对于大的项目来说,用PyQT/QT可能会是一个更加专业的选择。
本文首发链接为:https://www.cnblogs.com/dechinphy/p/tkinter1.html
作者ID:DechinPhy