图形用户界面(Graphical User Interface,简称GUI)是人与计算机之间产生交互的重要手段,它通过图形化的方式来呈现信息和数据,更加直观的展现计算机应用程序的数据和处理流程。Tkinter是Python语言自带的,用于创建GUI应用的标准Python库。
一、Tkinter的基本使用
1、安装Python以及Tkinter库
>>> # 检查Python版本和是否已安装Tkinter库
>>> import tkinter
>>> tkinter._test()
2、构建GUI应用程序的基本框架
>>> import tkinter as tk
>>> window = tk.Tk() # 创建窗口
>>> window.title('Python GUI应用') # 设置窗口标题
>>> window.geometry('400x300') # 设置窗口大小
>>> window.mainloop() # 运行窗口
3、向窗口中添加控件,比如Label、Button、Entry等等
>>> import tkinter as tk
>>> window = tk.Tk() # 创建窗口
>>> window.title('Python GUI应用') # 设置窗口标题
>>> window.geometry('400x300') # 设置窗口大小
>>> lbl = tk.Label(window, text='Hello, Tkinter!', font=('Arial', 12)) # 添加标签
>>> lbl.pack() # 将标签添加到窗口中并自适应大小
>>> btn = tk.Button(window, text='Click me!', width=10, height=2, command=None) # 添加按钮
>>> btn.pack() # 将按钮添加到窗口中并自适应大小
>>> window.mainloop() # 运行窗口
二、Tkinter的布局管理
1、使用pack方法进行布局
>>> import tkinter as tk
>>> window = tk.Tk() # 创建窗口
>>> window.title('Python GUI应用') # 设置窗口标题
>>> window.geometry('400x300') # 设置窗口大小
>>> lbl1 = tk.Label(window, text='Hello, Tkinter!', font=('Arial', 12)) # 添加标签
>>> lbl1.pack(side='top') # 将标签添加到窗口顶部
>>> lbl2 = tk.Label(window, text='Welcome to Tkinter!', font=('Arial', 12)) # 添加标签
>>> lbl2.pack(side='bottom') # 将标签添加到窗口底部
>>> btn = tk.Button(window, text='Click me!', width=10, height=2, command=None) # 添加按钮
>>> btn.pack(side='left') # 将按钮添加到窗口左侧
>>> window.mainloop() # 运行窗口
2、使用grid方法进行布局
>>> import tkinter as tk
>>> window = tk.Tk() # 创建窗口
>>> window.title('Python GUI应用') # 设置窗口标题
>>> window.geometry('400x300') # 设置窗口大小
>>> for i in range(3):
for j in range(3):
tk.Label(window, text='%d,%d' % (i, j), width=10, height=2, bg='white').grid(row=i, column=j)
tk.mainloop()
三、Tkinter的事件绑定
1、使用command参数进行事件绑定
>>> import tkinter as tk
>>> def show_info():
print('Hello, Tkinter!')
>>> window = tk.Tk() # 创建窗口
>>> window.title('Python GUI应用') # 设置窗口标题
>>> window.geometry('400x300') # 设置窗口大小
>>> btn = tk.Button(window, text='Click me!', width=10, height=2, command=show_info) # 添加按钮并绑定事件
>>> btn.pack() # 将按钮添加到窗口中并自适应大小
>>> window.mainloop() # 运行窗口
2、使用bind方法进行事件绑定
>>> import tkinter as tk
>>> def show_info(event):
print('Hello, Tkinter!')
>>> window = tk.Tk() # 创建窗口
>>> window.title('Python GUI应用') # 设置窗口标题
>>> window.geometry('400x300') # 设置窗口大小
>>> lbl = tk.Label(window, text='Hello, Tkinter!', font=('Arial', 12)) # 添加标签
>>> lbl.pack() # 将标签添加到窗口中并自适应大小
>>> lbl.bind('
', show_info) # 绑定标签的左键单击事件
>>> window.mainloop() # 运行窗口
四、Tkinter的常用控件
1、Label控件:用于展示文本、图片等信息
>>> import tkinter as tk
>>> window = tk.Tk() # 创建窗口
>>> window.title('Python GUI应用') # 设置窗口标题
>>> window.geometry('400x300') # 设置窗口大小
>>> lbl1 = tk.Label(window, text='Hello, Tkinter!')
>>> lbl1.pack()
>>> img = tk.PhotoImage(file='path/to/image.png')
>>> lbl2 = tk.Label(window, image=img)
>>> lbl2.pack()
>>> window.mainloop()
2、Button控件:用于响应用户点击事件
>>> import tkinter as tk
>>> def show_info():
print('Hello, Tkinter!')
>>> window = tk.Tk() # 创建窗口
>>> window.title('Python GUI应用') # 设置窗口标题
>>> window.geometry('400x300') # 设置窗口大小
>>> btn1 = tk.Button(window, text='Click me!', command=show_info)
>>> btn1.pack()
>>> btn2 = tk.Button(window, text='Exit', command=window.destroy)
>>> btn2.pack()
>>> window.mainloop()
3、Entry控件:用于获取用户输入信息
>>> import tkinter as tk
>>> def show_info():
print(entry.get())
>>> window = tk.Tk() # 创建窗口
>>> window.title('Python GUI应用') # 设置窗口标题
>>> window.geometry('400x300') # 设置窗口大小
>>> entry = tk.Entry(window)
>>> entry.pack()
>>> btn = tk.Button(window, text='Click me!', command=show_info)
>>> btn.pack()
>>> window.mainloop()
五、Tkinter的样式配置
1、使用config方法进行样式配置
>>> import tkinter as tk
>>> window = tk.Tk() # 创建窗口
>>> window.title('Python GUI应用') # 设置窗口标题
>>> window.geometry('400x300') # 设置窗口大小
>>> lbl = tk.Label(window, text='Hello, Tkinter!')
>>> lbl.config(fg='blue', bg='yellow', font=('Arial', 12))
>>> btn = tk.Button(window, text='Click me!')
>>> btn.config(width=10, height=2, fg='white', bg='green')
>>> entry = tk.Entry(window)
>>> entry.config(width=20, font=('Arial', 12))
>>> window.mainloop()
2、使用style方法进行样式配置
>>> import tkinter as tk
>>> from tkinter import ttk
>>> window = tk.Tk() # 创建窗口
>>> window.title('Python GUI应用') # 设置窗口标题
>>> window.geometry('400x300') # 设置窗口大小
>>> style = ttk.Style()
>>> style.configure('TLabel', foreground='blue', background='yellow', font=('Arial', 12))
>>> style.configure('TButton', width=10, height=2, foreground='white', background='green')
>>> style.configure('TEntry', width=20, font=('Arial', 12))
>>> lbl = ttk.Label(window, text='Hello, Tkinter!')
>>> lbl.pack()
>>> btn = ttk.Button(window, text='Click me!')
>>> btn.pack()
>>> entry = ttk.Entry(window)
>>> entry.pack()
>>> window.mainloop()
六、Tkinter的常用功能
1、消息对话框的使用
>>> import tkinter as tk
>>> from tkinter import messagebox
>>> window = tk.Tk() # 创建窗口
>>> window.title('Python GUI应用') # 设置窗口标题
>>> window.geometry('400x300') # 设置窗口大小
>>> def show_info():
messagebox.showinfo('消息提示', 'Hello, Tkinter!')
>>> btn = tk.Button(window, text='Click me!', command=show_info)
>>> btn.pack()
>>> window.mainloop()
2、文件对话框的使用
>>> import tkinter as tk
>>> from tkinter import filedialog
>>> window = tk.Tk() # 创建窗口
>>> window.title('Python GUI应用') # 设置窗口标题
>>> window.geometry('400x300') # 设置窗口大小
>>> def open_file():
file_path = filedialog.askopenfilename(filetypes=[('All Files', '*.*')])
print(file_path)
>>> btn = tk.Button(window, text='Open file', command=open_file)
>>> btn.pack()
>>> window.mainloop()