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

如何在含多列Entry的Tkinter Frame中添加水平滚动条?

问题描述

我编写了如下Tkinter代码:

from tkinter import *

Main = Tk()
# Main window
Main.title("Program")
Main.geometry("900x520")
# Background photo placed on top of Main
Photo_BG = PhotoImage(file=r"..//\Pictures\Background2.png")
Background_Main = Canvas(Main, width=800, height=400)
Background_Main.place(x=1, y=1)
# Frame One placed on top of Background photo. Frame One will be used to place widgets on it
Frame_BG = PhotoImage(file=r"..//\Pictures\Frame.png")
Frame_one = Frame(Background_Main, width=300, height=200)
Frame_one.place(x=1, y=1)
Label_Frame_one = Label(Frame_one, image = Frame_BG)
Label_Frame_one.place(x=1, y=1)
# Horizontal Scrollbar for the Frame One to Scroll Entry Columns
hbar = Scrollbar(Frame_one,orient = 'horizontal',width= 20)

for i in range(9): # columns
    for j in range(4): # rows
        entry_grid = Entry(Frame_one, width=7, fg='black', font=('Arial',10,'bold'))
        entry_grid.place(x=((i*60)+1), y=((j*20)+1))
        entry_grid.insert(END, str(i+j))
        entry_grid.config(state= "disabled")
        
entry_grid.config(xscrollcommand = hbar.set)
hbar.place(x=1, y=90)
Main.mainloop()

当前输出效果:
当前输出界面

我希望仅在Frame_one内实现列的水平滚动,且要求使用place布局而非pack布局,该如何操作?

解决方案

要实现Frame_one内的水平滚动且用place布局,核心是用Canvas作为可滚动载体,把放置Entry的子Frame嵌入Canvas,再绑定滚动条控制Canvas的水平显示区域。具体实现如下:

修改后的完整代码

from tkinter import *

Main = Tk()
Main.title("Program")
Main.geometry("900x520")

# 背景部分保留原逻辑,修正背景图显示方式
Photo_BG = PhotoImage(file=r"..//\Pictures\Background2.png")
Background_Main = Canvas(Main, width=800, height=400)
Background_Main.place(x=1, y=1)
Background_Main.create_image(0, 0, image=Photo_BG, anchor="nw")

# 外层Frame_one保留,修正背景标签铺满容器
Frame_BG = PhotoImage(file=r"..//\Pictures\Frame.png")
Frame_one = Frame(Background_Main, width=300, height=200)
Frame_one.place(x=1, y=1)
Label_Frame_one = Label(Frame_one, image=Frame_BG)
Label_Frame_one.place(x=1, y=1, relwidth=1, relheight=1)

# 创建滚动用的Canvas和内部承载Entry的Frame
scroll_canvas = Canvas(Frame_one, width=290, height=100, bg="white")
inner_frame = Frame(scroll_canvas)

# 配置水平滚动条,绑定Canvas的xview方法
hbar = Scrollbar(Frame_one, orient='horizontal', width=20, command=scroll_canvas.xview)
scroll_canvas.configure(xscrollcommand=hbar.set)

# 用place布局Canvas和滚动条
scroll_canvas.place(x=5, y=5)
hbar.place(x=5, y=110, width=290)

# 将inner_frame嵌入Canvas
inner_window = scroll_canvas.create_window(0, 0, window=inner_frame, anchor="nw")

# 在inner_frame中放置所有Entry组件
for i in range(9):  # columns
    for j in range(4):  # rows
        entry_grid = Entry(inner_frame, width=7, fg='black', font=('Arial',10,'bold'))
        entry_grid.place(x=((i*60)+1), y=((j*20)+1))
        entry_grid.insert(END, str(i+j))
        entry_grid.config(state="disabled")

# 动态更新Canvas的滚动区域,确保所有Entry都能被滚动到
def update_scroll_region(event):
    scroll_canvas.configure(scrollregion=scroll_canvas.bbox("all"))

inner_frame.bind("<Configure>", update_scroll_region)

Main.mainloop()

核心改动说明

  • 容器重构:新增scroll_canvas作为滚动容器,inner_frame专门承载Entry组件,滚动时实际是移动Canvas的显示区域,而非直接滚动Frame。
  • 滚动绑定:滚动条的command关联Canvas的xview方法,Canvas的xscrollcommand关联滚动条的set方法,实现滚动联动。
  • 滚动范围更新:绑定inner_frame的<Configure>事件,动态调整Canvas的滚动区域,确保所有Entry都在可滚动范围内。
  • 布局适配:所有组件均使用place布局,同时修正了背景图和背景标签的显示逻辑,确保容器铺满效果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 02:20:15