带网格的 tkinter 滚动条,它没有链接

tkinter scrollbar with grid, it doesn't get linked

我正在研究GUI,所以请理解我下面的代码。

我正在尝试制作一个获取游戏角色信息的程序。因此,如果您按下 'search' 按钮,列表将显示在下方。但是...由于 window 大小,它只显示大约 11 个名称。所以我想为那个区域放一个滚动条,但我就是不知道如何link滚动条来控制这个区域。我的意思是,滚动条本身已经创建,它会自己滚动,但不会滚动我想要的 window。我一定是 link 弄错了,但是...不确定。

下面是最小化的示例代码,但它仍然很长而且很粗糙。再次抱歉。 如果有人能开导我,这对我和我以后的工作都会有很大的帮助。

import tkinter as tk

import requests
from bs4 import BeautifulSoup
import webbrowser
import time

global var_dict, input_id, output
var_dict = {}

def enter_call_back(event=None):
    output.grid(column = 0, row = 2, columnspan = 5 , sticky='w')
    output.insert(tk.END,"Text_Something")
    output.update()
    search_chr()

def open_browse(url_list):
    for url in url_list:
        time.sleep(0.3)
        webbrowser.open(url)

def search_inven(ch_id):
    
    if ch_id == "ch1" or ch_id == "ch13" or ch_id == "ch15" :
        num = 5
        url_list = ["something.url","something2.url"]
        self_count = 1
    else:
        num = 0
        url_list = []
        self_count = 0
        
    masterset = []
    masterset.append(num)
    masterset.append(url_list)
    masterset.append(self_count)
        
       
    return masterset

def search_chr():
    global var_dict, output
    
    for things in var_dict.keys():
        var_dict[things].destroy()
        
    chr_list = ["ch1","ch2","ch3","ch4","ch5","ch6","ch7","ch8","ch9","ch9","ch10","ch11","ch12","ch13","ch14","ch15"]
    
    output.insert(tk.END," Done! \n\n")
    
    var_dict = {}
    num = -1
    for ch in chr_list:
        num += 1
        var_dict["output%s" %num] = tk.Entry(frame_buttons, width = 125)
        
        result = search_inven(ch)

        if result[0] == 0:
            var_dict["output"+str(num)].insert(0, "Clean "+ch+"\n")
            var_dict["output"+str(num)].grid(column = 0, row = num, sticky='w', padx=5, pady=5)
        else:
            
            url_list = result[1]
            var_dict["o-button%s" %num] = tk.Button(frame_buttons, command=lambda url_list = url_list : open_browse(url_list))
            var_dict["o-button"+str(num)].grid(column = 1, row = num, sticky='e')
            var_dict["o-button"+str(num)].config(text="URL")
            
            var_dict["output"+str(num)].insert(0, "Not Clean "+str(result[0])+" Self : "+str(result[2])+" Ch_id : "+ch+")\n")
            var_dict["output"+str(num)].grid(column = 0, row = num, sticky='w', padx=5, pady=5)
            
    vsb = tk.Scrollbar(frame_canvas, orient="vertical")
    vsb.grid(row=0, column=1, sticky='ns')
    vsb.config(command=canvas.yview)
    canvas.configure(yscrollcommand=vsb.set)
    
    frame_canvas.config(height = 300)
    canvas.config(scrollregion=canvas.bbox("all"))
    

root = tk.Tk()
root.geometry("760x710")
root.resizable(width=False, height=False)
root.title("Minimum v.1.2")
root.grid_rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)

frame_main = tk.Frame(root, bg="gray")
frame_main.grid(sticky='news')

intro = tk.Text(frame_main, height = 17, bg="#E3D5F3")
intro.option_add("*Font", "명조 10")
intro.insert(tk.CURRENT,"Text_something")
intro.config(state='disabled')
intro.grid(row=0, column=0, pady=(5, 0), columnspan = 5, sticky='nw')

input_id = tk.Entry(frame_main, width = 35)
input_id.option_add("*Font","명조 10")
input_id.insert(0,"Ch_name")
input_id.grid(row=1, column=0, pady=(5, 0), sticky='w')

search_btn = tk.Button(frame_main)
search_btn.config(text="Search")
search_btn.option_add("*Font","명조 10")
search_btn.config(width=5,height=1)
search_btn.grid(row = 1, column = 0, pady=(5, 0), sticky='e')

output = tk.Text(frame_main, height = 10)
output.option_add("*Font","명조 10")
output.grid(row = 2, column = 0,pady=(5,0),sticky='nw')


frame_canvas = tk.Frame(frame_main, width = 565)
frame_canvas.grid(row=3, column=0, pady=(5, 0), columnspan = 3 ,sticky='nw')
frame_canvas.grid_rowconfigure(0, weight=1)
frame_canvas.grid_columnconfigure(0, weight=1)
frame_canvas.grid_propagate(False)

canvas = tk.Canvas(frame_canvas, bg="gray", height=500,scrollregion=(0,0,500,1800))
canvas.grid(row=0, column=0, sticky="news")


frame_buttons = tk.Frame(canvas, bg="gray")
frame_buttons.grid(row = 0, column = 0,sticky='e')


root.bind('<Return>',enter_call_back)
search_btn.config(command = enter_call_back)


root.mainloop()

首先,使用grid()frame_buttons放入canvas,不会影响canvas的scrollregion。请改用 canvas.create_window()

其次,最好在 frame_buttons 上绑定 <Configure> 事件,并在绑定回调中更新 canvas' scrollregion。然后将小部件添加到 frame_buttons 将自动更新 scrollregion.

另请注意,您已经创建了新的 scrollbar 并在执行 search_chr() 时将其放在同一位置。最好只在函数外创建一次 scrollbar