如何在具有多个条目列的 Tkinter 框架中插入水平滚动条?

问题描述 投票:0回答:1

我有以下代码:

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()

这是输出:

enter image description here

如何设法仅在 Frame_One 中的列之间水平滚动?我想使用场地属性,而不是包。

python tkinter scrollbar frame
1个回答
0
投票

基于此链接和评论的帮助,我设法找到了我正在寻找的内容。

from tkinter import * 

def onFrameConfigure(event):
    '''Reset the scroll region to encompass the inner frame'''
    Background_Main.configure(scrollregion=Background_Main.bbox("all"))

# Creating Main Window
Main = Tk()
Main.geometry("500x400")
Main.title("Program")
# Loading Background Photo
Photo_BG = PhotoImage(file=r"..//\Pictures\Frame.png")
# Loading Canvas on top on Main window
Background_Main = Canvas(Main, borderwidth=0, background="#ffffff", width=400, height=300)
Background_Main.create_image(0,0,image=Photo_BG, anchor='nw')
Background_Main.place(x=0, y=0)
# Creating Frame_one on top of Background_Main
Frame_one = Frame(Background_Main, background="#ffffff")
# Creating horizontal and a vertical Scrollbar on top of Background_Main
vsb = Scrollbar(Background_Main, orient="vertical", command=Background_Main.yview)
hsb = Scrollbar(Background_Main, orient="horizontal", command=Background_Main.xview)
# Populate Grid Entry with fake data
for i in range(33): # columns
    for j in range(33): # rows
        entry_grid = Entry(Frame_one, width=7, fg='black', font=('Arial',10,'bold'))
        entry_grid.grid(row=j, column=i)
        entry_grid.insert(END, "N/A")
# Configuring  scrollbar commands
Background_Main.configure(yscrollcommand=vsb.set)
Background_Main.configure(xscrollcommand=vsb.set)
# Placing the Scrollbars on the Background_Main
vsb.place(x=0, y=0, height=300, width=20)  #<----- height and width option can resize scrollbar
hsb.place(x=20, y=0, height=20, width=380) #<----- height and width option can resize scrollbar 
# Creating an editable window on top of Background_Main
Background_Main.create_window((20,20), window=Frame_one, anchor="nw",
                       tags="Frame_one")

Frame_one.bind("<Configure>", onFrameConfigure)

Main.mainloop()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.