python tk实现标签切换页面

47 篇文章 0 订阅
import tkinter as tk
from tkinter import ttk

# 初始化主窗口
root = tk.Tk()
root.title("标签页示例")

# 设置窗口大小
root.geometry("400x300")

# 创建 Notebook 小部件
notebook = ttk.Notebook(root)
notebook.pack(expand=True, fill="both")

# 创建两个Frame,作为两个标签页的内容
page1 = ttk.Frame(notebook)
page2 = ttk.Frame(notebook)
page3 = ttk.Frame(notebook)

# 将Frame添加到Notebook中,同时设置标签名称
notebook.add(page1, text="页面 1")
notebook.add(page2, text="页面 2")
notebook.add(page3, text="页面 3")

# 页面1的内容
# 单选框
radio_var = tk.IntVar()
radio1 = ttk.Radiobutton(page1, text="选项1", variable=radio_var, value=1)
radio2 = ttk.Radiobutton(page1, text="选项2", variable=radio_var, value=2)
radio3 = ttk.Radiobutton(page1, text="选项3", variable=radio_var, value=3)
radio1.grid(row=0, column=0, sticky=tk.W)
radio2.grid(row=1, column=0, sticky=tk.W)
radio3.grid(row=2, column=0, sticky=tk.W)

# 输入框
entry1 = ttk.Entry(page1)
entry2 = ttk.Entry(page1)
entry1.grid(row=3, column=0, padx=20, pady=5)
entry2.grid(row=4, column=0, padx=20, pady=5)

# 按钮
button1 = ttk.Button(page1, text="按钮1")
button2 = ttk.Button(page1, text="按钮2")
button3 = ttk.Button(page1, text="按钮3")
button1.grid(row=5, column=0, pady=5)
button2.grid(row=5, column=1, pady=5)
button3.grid(row=5, column=2, pady=5)

# 页面2的内容
# 标签文本
label1 = ttk.Label(page2, text="这是页面 2 的标签1")
label2 = ttk.Label(page2, text="这是页面 2 的标签2")
label1.pack(pady=5)
label2.pack(pady=5)

# 输入框
entry_page2 = ttk.Entry(page2)
entry_page2.pack(pady=5)

# 按钮
button_page2 = ttk.Button(page2, text="页面2的按钮")
button_page2.pack(pady=5)

# 运行主循环
root.mainloop()

python tk实现选项卡点击按钮左右切换,窗口展示当前页面标题-CSDN博客

import tkinter as tk
from tkinter import ttk

# 初始化主窗口
root = tk.Tk()
root.title("标签页示例")

# 设置窗口大小
root.geometry("400x300")

# 创建 Notebook 小部件
notebook = ttk.Notebook(root)
notebook.pack(expand=True, fill="both")

# 创建两个Frame,作为两个标签页的内容
page1 = ttk.Frame(notebook)
page2 = ttk.Frame(notebook)
page3 = ttk.Frame(notebook)

# 将Frame添加到Notebook中,同时设置标签名称
notebook.add(page1, text="页面 1")
notebook.add(page2, text="页面 2")
notebook.add(page3, text="页面 3")


# 包装Notebook控件
notebook.pack(expand=1, fill='both')

# 全局变量用于存储Notebook
global_notebook = notebook


def update_title():
    # 更新窗口标题为当前选项卡的名称
    root.title(global_notebook.tab('current')['text'])


def switch_left():
    current_index = global_notebook.index('current')
    if current_index > 0:
        global_notebook.select(global_notebook.tabs()[current_index - 1])
    else:
        # 如果在第一个选项卡上,切换到最后一个选项卡
        global_notebook.select(global_notebook.tabs()[-1])
    update_title()


def switch_right():
    current_index = global_notebook.index('current')
    if current_index < len(global_notebook.tabs()) - 1:
        global_notebook.select(global_notebook.tabs()[current_index + 1])
    else:
        # 如果在最后一个选项卡上,切换到第一个选项卡
        global_notebook.select(global_notebook.tabs()[0])
    update_title()


# 创建按钮用于切换选项卡
button_left = ttk.Button(root, text="Left", command=switch_left)
button_left.pack(side='left')
button_right = ttk.Button(root, text="Right", command=switch_right)
button_right.pack(side='right')


# 页面1的内容
# 单选框
radio_var = tk.IntVar()
radio1 = ttk.Radiobutton(page1, text="选项1", variable=radio_var, value=1)
radio2 = ttk.Radiobutton(page1, text="选项2", variable=radio_var, value=2)
radio3 = ttk.Radiobutton(page1, text="选项3", variable=radio_var, value=3)
radio1.grid(row=0, column=0, sticky=tk.W)
radio2.grid(row=1, column=0, sticky=tk.W)
radio3.grid(row=2, column=0, sticky=tk.W)

# 输入框
entry1 = ttk.Entry(page1)
entry2 = ttk.Entry(page1)
entry1.grid(row=3, column=0, padx=20, pady=5)
entry2.grid(row=4, column=0, padx=20, pady=5)

# 按钮
button1 = ttk.Button(page1, text="按钮1")
button2 = ttk.Button(page1, text="按钮2")
button3 = ttk.Button(page1, text="按钮3")
button1.grid(row=5, column=0, pady=5)
button2.grid(row=5, column=1, pady=5)
button3.grid(row=5, column=2, pady=5)

# 页面2的内容
# 标签文本
label1 = ttk.Label(page2, text="这是页面 2 的标签1")
label2 = ttk.Label(page2, text="这是页面 2 的标签2")
label1.pack(pady=5)
label2.pack(pady=5)

# 输入框
entry_page2 = ttk.Entry(page2)
entry_page2.pack(pady=5)

# 按钮
button_page2 = ttk.Button(page2, text="页面2的按钮")
button_page2.pack(pady=5)


# 初始更新标题
update_title()


# 运行主循环
root.mainloop()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值