简介

tkinter 是 Python 中的标准 GUI(图形用户界面)库,它提供了创建窗口、按钮、文本框等常见 GUI 元素的功能。tkinter 基于 Tk GUI 工具包,并且是 Python 标准库的一部分,因此无需额外安装。

以下是 tkinter 的一些基本概念和用法:

  1. 导入 tkinter 模块

    1
    import tkinter as tk
  2. 创建主窗口

    1
    root = tk.Tk()

    Tk() 创建了一个主窗口对象。

  3. 添加组件

    tkinter 提供了各种组件(widgets),如按钮、标签、文本框等。你可以使用这些组件构建你的 GUI。

    1
    2
    label = tk.Label(root, text="Hello, tkinter!")
    button = tk.Button(root, text="Click me!", command=callback_function)

    在上面的例子中,Label 是一个标签,用于显示文本;Button 是一个按钮,通过 command 参数关联一个回调函数,当按钮被点击时执行。

  4. 布局管理

    tkinter 提供了不同的布局管理器,如 packgridplace,用于控制组件的位置和大小。

    1
    2
    label.pack()  # 将标签放置在主窗口中
    button.grid(row=0, column=1) # 将按钮放置在主窗口的网格中
  5. 事件处理

    通过为组件关联事件处理函数,可以在用户与 GUI 交互时执行特定的操作。例如,按钮的点击事件可以通过 command 参数或 bind 方法来处理。

    1
    2
    3
    4
    def callback_function():
    print("Button clicked!")

    button = tk.Button(root, text="Click me!", command=callback_function)
  6. 启动主循环

    1
    root.mainloop()

    这是 GUI 应用程序的主循环,它等待用户交互并响应事件。

详细的参考

https://sunhwee.com/posts/80fa3a85.html

简单示例

以下是一个简单的 tkinter 示例,演示了一个包含标签、按钮和文本框的简单 GUI 窗口。这个示例创建一个窗口,用户可以在文本框中输入文本,然后点击按钮,标签将显示输入的文本。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import tkinter as tk

def update_label():
# 从文本框获取输入文本
input_text = entry.get()

# 更新标签的文本
label.config(text=f"你输入的是: {input_text}")

# 创建主窗口
root = tk.Tk()
root.title("Simple Tkinter Example")

# 创建标签
label = tk.Label(root, text="输入内容并点击按钮.")
label.pack(pady=10)

# 创建文本框
entry = tk.Entry(root, width=30)
entry.pack(pady=10)

# 创建按钮,并关联回调函数
button = tk.Button(root, text="Update Label", command=update_label)
button.pack(pady=10)

# 启动主循环
root.mainloop()