Tkinter 中带有滚动条的框架?

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

所以我试图制作一个框架并用数据库中的数据填充它。唯一的问题是,我需要整个框架可以滚动。我按照我在网上看到的说明进行操作,还检查了 Stackoverflow,但我找不到我的代码有什么问题。当我运行程序时,它不会在框架的任何位置显示滚动条。

showWindow = Tk()
showWindow.title("Product List")
showWindow.geometry("500x250")
Main_Frame = Frame(showWindow)
Main_Frame.pack(fill=BOTH,expand=TRUE)
CanvasA = Canvas(Main_Frame)
CanvasA.pack(fill=BOTH,expand=TRUE)
ProductScroller = Scrollbar(Main_Frame,orient=VERTICAL,
                command= CanvasA.yview)
ProductScroller.pack(side=RIGHT,fill=Y)
CanvasA.configure(yscrollcommand=ProductScroller.set)
CanvasA.bind('<Configure>', lambda e: CanvasA.configure(scrollregion=CanvasA.bbox("all")))
InsideFrame = Frame(CanvasA)
CanvasA.create_window((0,0),window= InsideFrame, anchor= "center")

conn = sqlite3.connect('Database')
C = conn.cursor()
C.execute("Select *, oid from Database")
ProductRecords = C.fetchall()
HEADER = LabelFrame(InsideFrame)
HEADER.pack(side=TOP, fill=X, expand=TRUE)
NameH = Label(HEADER, text="Name")
NameH.pack(side=LEFT, fill=BOTH, expand=TRUE)
CostH = Label(HEADER, text="Cost")
CostH.pack(side=LEFT, fill=BOTH, expand=TRUE)
PriceH = Label(HEADER, text="Price")
PriceH.pack(side=LEFT, fill=BOTH, expand=TRUE)
RetailH = Label(HEADER, text="Retail")
RetailH.pack(side=LEFT, fill=BOTH, expand=TRUE)
for record in ProductRecords:
    NewRow= LabelFrame(InsideFrame)
    NewRow.pack(side=BOTTOM, fill = X, expand= TRUE)
    NameR = Label(NewRow,text=record[0])
    NameR.pack(side=LEFT,fill=X,expand=TRUE)
    CostR = Label(NewRow, text=record[1])
    CostR.pack(side=LEFT, fill=X, expand=TRUE)
    PriceR = Label(NewRow, text=record[2])
    PriceR.pack(side=LEFT, fill=X, expand=TRUE)
    PriceR = Label(NewRow, text=record[3])
    PriceR.pack(side=LEFT, fill=X, expand=TRUE)

conn.commit()
conn.close()
python user-interface tkinter scrollbar tkinter-canvas
1个回答
0
投票

您需要在

side=LEFT
中设置
CanvasA.pack(...)

另外,

<Configure>
应该绑定在
InsideFrame
上,而不是
CanvasA
上,因为当调整内部框架大小时,画布的
scrollregion
应该更新。

...
CanvasA.pack(side=LEFT,fill=BOTH,expand=TRUE)
...
#CanvasA.bind('<Configure>', lambda e: CanvasA.configure(scrollregion=CanvasA.bbox("all")))
...
# bind <Configure> on InsideFrame
InsideFrame.bind('<Configure>', lambda e: CanvasA.configure(scrollregion=CanvasA.bbox("all")))
CanvasA.create_window((0,0),window= InsideFrame, anchor= "nw") # use "nw" instead of "center"
...
© www.soinside.com 2019 - 2024. All rights reserved.