You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tkinter GUI中设置滚动条保持与窗口比例不变的方法

Tkinter垂直滚动条窗口缩放时保持比例的解决方法

问题说明

我是Tkinter新手,开发GUI时添加了垂直滚动条,但调整窗口尺寸时,滚动条的宽度会被拉伸(比例变形),希望滚动条保持固定宽度,始终和窗口垂直方向对齐,可使用任意几何管理器。

原代码如下:

import tkinter as tk


class GUI():
    def __init__(self):
        self.HEIGHT = 750
        self.WIDTH = 700
        self.play_pause = False

        self.root = tk.Tk()
        self.root.title("GUI window")
        self.root.geometry(newGeometry=f"{self.WIDTH}x{self.HEIGHT}")
        self.root.minsize(self.WIDTH, self.HEIGHT)

        self.frame3 = tk.LabelFrame(self.root, text="Output window", height=450, width=700)
        self.frame3.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)

        self.output_text = tk.Text(self.frame3, height=42, fg="white", bg="black")
        self.output_text.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

        v = tk.Scrollbar(self.frame3, orient='vertical', command=self.output_text.yview)
        v.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)


if __name__ == '__main__':
    app = GUI()
    app.root.mainloop()

问题原因

原代码中滚动条使用了pack(side=tk.RIGHT, fill=tk.BOTH, expand=True),其中expand=True会让滚动条在水平方向跟随窗口扩张,导致宽度被拉伸;同时frame3设置了固定宽高,干扰布局自适应。另外,文本框和滚动条未双向关联,滚动条无法同步文本框的滚动状态。

解决方案

方案1:修改Pack布局参数(最简单)

调整滚动条的Pack参数,去掉expand=True,只保留垂直填充;移除frame3的固定宽高,添加文本框与滚动条的双向关联:

修改后的完整代码:

import tkinter as tk


class GUI():
    def __init__(self):
        self.HEIGHT = 750
        self.WIDTH = 700
        self.play_pause = False

        self.root = tk.Tk()
        self.root.title("GUI window")
        self.root.geometry(f"{self.WIDTH}x{self.HEIGHT}")
        self.root.minsize(self.WIDTH, self.HEIGHT)

        # 移除固定宽高,让Frame自适应窗口
        self.frame3 = tk.LabelFrame(self.root, text="Output window")
        self.frame3.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)

        self.output_text = tk.Text(self.frame3, fg="white", bg="black")
        self.output_text.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

        # 滚动条仅垂直填充,不水平扩张
        v = tk.Scrollbar(self.frame3, orient='vertical', command=self.output_text.yview)
        v.pack(side=tk.RIGHT, fill=tk.Y)
        # 关联文本框与滚动条,实现双向控制
        self.output_text.config(yscrollcommand=v.set)


if __name__ == '__main__':
    app = GUI()
    app.root.mainloop()

方案2:使用Grid布局(更规整)

通过Grid的列权重分配空间,让文本框占满剩余区域,滚动条固定宽度:

修改后的核心布局代码:

# 配置Frame的Grid布局:第0列占满剩余空间,第0行占满垂直空间
self.frame3 = tk.LabelFrame(self.root, text="Output window")
self.frame3.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
self.frame3.grid_columnconfigure(0, weight=1)
self.frame3.grid_rowconfigure(0, weight=1)

# 文本框填充整个单元格
self.output_text = tk.Text(self.frame3, fg="white", bg="black")
self.output_text.grid(row=0, column=0, sticky="nsew")

# 滚动条仅垂直填充,宽度固定
v = tk.Scrollbar(self.frame3, orient='vertical', command=self.output_text.yview)
v.grid(row=0, column=1, sticky="ns")
self.output_text.config(yscrollcommand=v.set)

方案3:使用Place布局(灵活但不推荐)

直接指定滚动条的相对位置和尺寸,固定宽度并垂直填满父容器:

self.frame3 = tk.LabelFrame(self.root, text="Output window")
self.frame3.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)

self.output_text = tk.Text(self.frame3, fg="white", bg="black")
# 文本框留出滚动条宽度
self.output_text.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=(0, 20))

v = tk.Scrollbar(self.frame3, orient='vertical', command=self.output_text.yview)
# 滚动条固定在右侧,高度与Frame一致
v.place(relx=1.0, rely=0, relheight=1.0, anchor="ne")
self.output_text.config(yscrollcommand=v.set)

效果说明

以上方案调整后,无论如何拖动窗口改变大小,滚动条都会保持固定宽度,始终垂直填满右侧区域,比例不会发生变化,同时实现了滚动条与文本框的双向交互。

内容的提问来源于stack exchange,提问作者programmer_04_03

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 04:37:41