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

Python Tkinter窗口可调整大小但控件不随动,滚动框无法缩放求助

解决Tkinter滚动框无法缩放的问题

你的核心问题出在外层框架的布局配置上,body框架没有设置填充和扩展属性,导致它不会随窗口大小变化,内部的滚动区域自然也跟着固定尺寸。以下是具体修复步骤和完整代码:

关键修改点

  1. 调整header、body、footer的pack参数:
    • header固定在顶部,横向填满窗口:pack(side="top", fill=tk.X)
    • body占据中间所有可用空间,随窗口缩放:pack(side="top", fill=tk.BOTH, expand=True)
    • footer固定在底部,横向填满窗口:pack(side="bottom", fill=tk.X)
  2. 给滚动区域内的列设置权重:让复选框所在的列能填满滚动框宽度,避免内容挤在一侧。

修改后的完整代码

import tkinter as tk
from tkinter import ttk
import os
import subprocess

class Scrollable(tk.Frame):
    """
       Make a frame scrollable with scrollbar on the right.
       After adding or removing widgets to the scrollable frame,
       call the update() method to refresh the scrollable area.
    """

    def __init__(self, frame, width=16):
        scrollbar = tk.Scrollbar(frame, width=width)
        scrollbar.pack(side=tk.RIGHT, fill=tk.Y, expand=True)

        self.canvas = tk.Canvas(frame, yscrollcommand=scrollbar.set)
        self.canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

        scrollbar.config(command=self.canvas.yview)

        self.canvas.bind('<Configure>', self.__fill_canvas)

        # base class initialization
        tk.Frame.__init__(self, frame)

        # assign this obj (the inner frame) to the windows item of the canvas
        self.windows_item = self.canvas.create_window(0,0, window=self, anchor=tk.NW)


    def __fill_canvas(self, event):
        "Enlarge the windows item to the canvas width"
        canvas_width = event.width
        self.canvas.itemconfig(self.windows_item, width = canvas_width)

    def update(self):
        "Update the canvas and the scrollregion"
        self.update_idletasks()
        self.canvas.config(scrollregion=self.canvas.bbox(self.windows_item))

root = tk.Tk()
root.title("application")
root.geometry('750x800')
dbEnvs = ['a','b']
x = 1

header = ttk.Frame(root)
body = ttk.Frame(root)
footer = ttk.Frame(root)
# 修改pack参数,让各区域合理分配空间
header.pack(side="top", fill=tk.X)
body.pack(side="top", fill=tk.BOTH, expand=True)
footer.pack(side="bottom", fill=tk.X)


#setup Environment selection 
envLabel = tk.Label(header, text="Environment:")
envLabel.grid(row=0,column=0,sticky='nw')
dbselection = tk.StringVar()

scrollable_body = Scrollable(body, width=20)
x = 1
for row in range(50):
    checkboxVar = tk.IntVar()
    checkbox = ttk.Checkbutton(scrollable_body, text=row, variable=checkboxVar)
    checkbox.var = checkboxVar  # SAVE VARIABLE
    checkbox.grid(row=x, column=1, sticky='w')
    x += 1

# 设置列权重,让复选框列填满滚动框宽度
scrollable_body.grid_columnconfigure(1, weight=1)

scrollable_body.update()

#setup buttons on the bottom
pullBtn = tk.Button(footer, text='Pull')
pullBtn.grid(row=0, column=2, sticky='ew')
buildBtn = tk.Button(footer, text='Build')
buildBtn.grid(row=0, column=3, sticky='ew')
compBtn = tk.Button(footer, text='Compare')
compBtn.grid(row=0, column=4, sticky='ew')

# 给footer的列设置权重,让按钮随窗口缩放
footer.grid_columnconfigure(2, weight=1)
footer.grid_columnconfigure(3, weight=1)
footer.grid_columnconfigure(4, weight=1)

root.mainloop()

额外优化

我还给footer的按钮列加了权重,让底部的按钮也能随窗口宽度缩放,提升整体界面的适配性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 22:30:49