百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术分类 > 正文

Python GUI 编程:tkinter 初学者入门指南——窗格窗口

ztj100 2025-06-09 07:27 19 浏览 0 评论

在本文中,将介绍如何使用 tkinter PanedWindow 窗格窗口小部件。PanedWindow 小部件是一个容器,可以在窗体上创建可以调节大小的区域,这些区域称作窗格。

要创建小组件,请使用以下语法:

tk.PanedWindow(master, **options)

PanedWindow 常用选项:

选项

描述

bd

3D边框大小,默认不包含边框

bg

背景颜色

cursor

指定鼠标光标类型

borderwidth

边框宽度,默认值为 2 像素

handlepad

手柄和窗口边框距离,默认值为 8 像素

height

小部件高度

handlesize

手柄的大小,默认值为 8 像素

orient

并排放置子窗口,设置为 HORIZONTAL,从上到下放置子窗口,设置为 VERTICAL

sashpad

窗格填充

sashwidth

窗格边框宽度

sashrelief

窗格边框类型

showhandle

是否显示手柄,True/False

width

小组件的宽度

relief

边框的类型,默认值为 FLAT

PanedWindow 常用方法:

方法

描述

add(child,options)

为窗格添加其他小部件

forget(child)

删除子部件

panecget(child, option)

获得子组件指定选项的值

paneconfig(child, **options)

设置子组件的各种选项

PanedWindow 方法选项:

选项

描述

after

添加新的子组件到指定子组件后边

before

添加新的子组件到指定子组件前边

height

指定子组件的高度

minsize

控制窗格宽度/高度不得低于的值

padx

指定子组件的水平间距

pady

指定子组件的垂直间距

sticky

指定子组件位于窗格的位置(e、s、w、n,以及组合值)

width

指定子组件的宽度

示例:创建可调横向和纵向窗格

import tkinter as tk
root = tk.Tk()
root.geometry('600x400+200+200')
root.title('PanedWindow 窗格窗口演示')

pw = tk.PanedWindow(root, orient=tk.HORIZONTAL, showhandle=True)
pw1 = tk.PanedWindow(root, orient=tk.HORIZONTAL, showhandle=True)
left_Label = tk.Label(pw1, text="标签 1", bg="blue")
pw1.add(left_Label, width=200)

pw2 = tk.PanedWindow(pw, orient=tk.VERTICAL, showhandle=True)
top_Label = tk.Label(pw2, text="标签 2", bg="green")
pw2.add(top_Label, height=200)
bottom_Label = tk.Label(pw2, text="标签 3", bg="red")
pw2.add(bottom_Label)

pw.add(pw1)
pw.add(pw2)
pw.pack(fill=tk.BOTH, expand=True)

root.mainloop()

示例 :窗格窗口常用方法演示

import tkinter as tk
root = tk.Tk()
root.geometry('600x400+200+200')
root.title('PanedWindow 窗格窗口演示')

def add():
    label = tk.Label(pw1, text="标签", bg="lightblue")
    pw1.add(label, before=left_Label, width=80, minsize=50)
def remove():
    pw1.forget(left_Label) 
def get():
    print(pw2.panecget(top_Label, 'height'))
def conf():
    pw2.paneconfig(top_Label, height=80)

pw = tk.PanedWindow(root, orient=tk.HORIZONTAL, showhandle=True)

pw1 = tk.PanedWindow(root, orient=tk.HORIZONTAL, showhandle=True)
left_Label = tk.Label(pw1, text="标签 1", bg="blue")
pw1.add(left_Label, width=200)

pw2 = tk.PanedWindow(pw, orient=tk.VERTICAL, showhandle=True)
top_Label = tk.Label(pw2, text="标签 2", bg="green")
pw2.add(top_Label, height=200)
bottom_Label = tk.Label(pw2, text="标签 3", bg="red")
pw2.add(bottom_Label)

pw.add(pw1)
pw.add(pw2)
pw.pack(fill=tk.BOTH, expand=True)
frame = tk.Frame(root)
frame.pack()
tk.Button(frame, text="增加", command=add).pack(side=tk.LEFT, padx=10)
tk.Button(frame, text="删除", command=remove).pack(side=tk.LEFT, padx=10)
tk.Button(frame, text="获取", command=get).pack(side=tk.LEFT, padx=10)
tk.Button(frame, text="设置", command=conf).pack(side=tk.LEFT, padx=10)
root.mainloop()

add() 方法:为左侧窗格再添加 Label 小部件

forget() 方法:删除左侧窗格的 left_Label 小部件

panecget() 方法: 获取 top_Label 组件 height 选项的值,输出到控制台

paneconfig() 方法:设置 top_Label 组件 height 选项的值为 80

示例:窗格窗口中Text增加滚动条

import tkinter as tk
root = tk.Tk()
root.geometry('600x400+200+200')
root.title('PanedWindow 窗格窗口演示')

pw=tk.PanedWindow(root,orient=tk.VERTICAL,sashrelief='sunken') # 创建纵向窗格
pw.pack(fill=tk.BOTH,expand=True) 
top_frame = tk.Frame(pw)
tk.Label(top_frame, text='顶部Frame').pack() 
 
pw1=tk.PanedWindow(pw,orient=tk.HORIZONTAL,sashrelief='sunken') # 创建横向窗格
label = tk.Label(pw1,text='左侧标签',width=15)
right_frame = tk.Frame(pw1)
text = tk.Text(right_frame,wrap="none") # 右侧多行文本框
 
scrollbar_h = tk.Scrollbar(right_frame,orient=tk.HORIZONTAL,command=text.xview) # 创建滚动条
scrollbar_v = tk.Scrollbar(right_frame,command=text.yview)
scrollbar_h.pack(side=tk.BOTTOM,fill=tk.X)
scrollbar_v.pack(side=tk.RIGHT,fill=tk.Y)

text.pack(side=tk.LEFT,fill=tk.BOTH,expand=True) 
text.config(xscrollcommand=scrollbar_h.set,yscrollcommand=scrollbar_v.set)
pw1.add(label)
pw1.add(right_frame)
pw.add(top_frame,minsize=80)
pw.add(pw1) 
root.mainloop()

相关推荐

Java的SPI机制详解

作者:京东物流杨苇苇1.SPI简介SPI(ServiceProvicerInterface)是Java语言提供的一种接口发现机制,用来实现接口和接口实现的解耦。简单来说,就是系统只需要定义接口规...

90%的Java程序员都忽视的内部类使用不当导致内存泄露!

...

一文读懂 Spring Boot 启动原理,开发效率飙升!

在当今的Java开发领域,SpringBoot无疑是最热门的框架之一。它以其“约定大于配置”的理念,让开发者能够快速搭建和启动应用,极大地提高了开发效率。但是,你是否真正了解Spring...

ServiceLoader

ServiceLoader是Java提供的一种服务发现机制(ServiceProviderInterface,SPI)...

深入探索 Spring Boot3 中的自定义扩展操作

在当今互联网软件开发领域,SpringBoot无疑是最受欢迎的框架之一。随着其版本迭代至SpringBoot3,它为开发者们带来了更多强大的功能和特性,其中自定义扩展操作更是为我们在项目开发中...

Spring Boot启动过程全面解析:从入门到精通

一、SpringBoot概述SpringBoot是一个基于Spring框架的快速开发脚手架,它通过"约定优于配置"的原则简化了Spring应用的初始搭建和开发过程。...

Spring Boot 3.x 自定义 Starter 详解

今天星期六,继续卷springboot3.x。在SpringBoot3.x中,自定义Starter是封装和共享通用功能、实现“约定优于配置”理念的强大机制。通过创建自己的Starte...

Spring Boot 的 3 种动态 Bean 注入技巧

在SpringBoot开发中,动态注入Bean是一种强大的技术,它允许我们根据特定条件或运行时环境灵活地创建和管理Bean。相比于传统的静态Bean定义,动态注入提供了更高的灵活性和可...

大佬用4000字带你彻底理解SpringBoot的运行原理!

SpringBoot的运行原理从前面创建的SpringBoot应用示例中可以看到,启动一个SpringBoot工程都是从SpringApplication.run()方法开始的。这个方法具体完成...

Springboot是如何实现自动配置的

SpringBoot的自动配置功能极大地简化了基于Spring的应用程序的配置过程。它能够根据类路径中的依赖和配置文件中的属性,自动配置应用程序。下面是SpringBoot实现自动配置的...

Spring Boot3.x 应用的生命周期深度解析

SpringBoot应用的生命周期可以清晰地划分为三个主要阶段:启动阶段(Startup)...

Springboot 启动流程及各类事件生命周期那点事

前言本文通过Springboot启动方法分析SpringApplication逻辑。从静态run方法执行到各个阶段发布不同事件完成整个应用启动。...

Spring框架基础知识-常用的接口1

BeanDefinition基本概念BeanDefinition是Spring框架中描述bean配置信息的核心接口,它包含了创建bean实例所需的所有元数据。...

一家拥有 158 年历史的公司遭遇索赔,被迫关闭!

...

Java 技术岗面试全景备战!从基础到架构的系统性通关攻略分享

Java技术岗的面试往往是一项多维度的能力检验。本文将会从核心知识点、项目经验到面试策略,为你梳理一份系统性的备战攻略!...

取消回复欢迎 发表评论: