简体   繁体   English

在tkinter中更改滚动条的大小(使用网格布局)

[英]Changing the size of a scrollbar in tkinter(using grid layout)

I'm making a Tkinter GUI that allows you to query a database and will display results in a scrollable frame. 我正在制作一个Tkinter GUI,它允许您查询数据库并将结果显示在可滚动的框架中。 However, when you produce the results the scrollbar will not adjust to match the new size of the frame. 但是,当您生成结果时,滚动条将不会调整以匹配框架的新大小。 How can I get the scrollbar to be able to display all of the results? 如何获取滚动条以显示所有结果? I've put together a quick and dirty version of the code to demonstrate the problem I'm having. 我整理了一个快速而肮脏的代码版本,以演示我遇到的问题。

import tkinter as tk

def Lookup():
    list = frame_buttons.grid_slaves()
    for l in list:
        l.destroy()
    for x in range(1000):
        tk.Label(frame_buttons, text="test", background="white").grid(row=x)


root = tk.Tk()
root.grid_rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)

frame_main = tk.Frame(root, bg="white")
frame_main.grid(row = 0,sticky='news')
frame_input = tk.Frame(frame_main, background = "white")
frame_input.grid(row=0, column=0, pady=(5, 0), sticky='nw')

tk.Button(frame_input, text="Search", fg="black", background = "grey",command=Lookup).grid(row=3, column=0,sticky='nw')

# Create a frame for the canvas with non-zero row&column weights
frame_canvas = tk.Frame(frame_main)
frame_canvas.grid(row=1, column=0, pady=(5, 0), sticky='nw')
frame_canvas.grid_rowconfigure(0, weight=1)
frame_canvas.grid_columnconfigure(0, weight=1)
# Set grid_propagate to False to allow 5-by-5 buttons resizing later
frame_canvas.grid_propagate(False)

# Add a canvas in that frame
canvas = tk.Canvas(frame_canvas, bg="gray")
canvas.grid(row=0, column=0, sticky="news")

# Link a scrollbar to the canvas
vsb = tk.Scrollbar(frame_canvas, orient="vertical", command=canvas.yview)
vsb.grid(row=0, column=1, sticky='ns')
canvas.configure(yscrollcommand=vsb.set)
frame_buttons = tk.Frame(canvas, bg="gray")
canvas.create_window((0, 0), window=frame_buttons, anchor='nw')
for x in range(15):
    tk.Label(frame_buttons, text="blah", background = "white").grid(row=x)
frame_buttons.update_idletasks()
frame_canvas.config(width=500, height=100)
canvas.config(scrollregion=canvas.bbox("all"))
root.mainloop()

this initially puts 20 labels in the scroll region, which is enough to activate the scrollbar. 这最初会在滚动区域中放置20个标签,足以激活滚动条。 Then when you click search it replaces those 20 lables with 1000 test labels. 然后,当您单击搜索时,它将用1000个测试标签替换这20个标签。 But only the first 20 will be viewable. 但是只有前20个可见。

You need to reset the scrollregion whenever the frame changes size and the items are redrawn. 每当框架更改大小并重新绘制项目时,都需要重置滚动区域。

The normal way to do that is to bind to the <Configure> event of the frame so that it happens automatically as the frame grows and shrinks. 这样做的通常方法是绑定到框架的<Configure>事件,以便它随着框架的增长和收缩而自动发生。

Example: 例:

def reset_scrollregion(event):
    canvas.config(scrollregion=canvas.bbox("all"))
...
frame_buttons.bind("<Configure>", reset_scrollregion)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM