如何在 Tkinter GUI 中应用滚动条?

How to apply a scrollbar in Tkinter GUI?

这个 python 程序的想法是从食品网站上抓取食谱,并在单击按钮时在 tkinter GUI 中输出食谱。我已经将滚动条应用到 GUI,但它没有正常工作。我已经尝试了很多方法,但没有成功,所以我决定向堆栈溢出社区寻求帮助。我是 tkinter 的新手,很抱歉为可能是一个愚蠢的问题寻求帮助。

目前的代码:

    import tkinter as tk
    from tkinter import Scrollbar

    root = tk.Tk()
    root.title('Italian-recipes')
    height=600
    width=895 

    canvas = tk.Canvas(root, height=height, width=width)
    canvas.pack()

    background_image = tk.PhotoImage(file='food.png')
    background_label = tk.Label(root, image=background_image)
    background_label.place(relwidth=1, relheight=1)

    lower_frame = tk.Frame(root, bg='#80c1ff', bd=5)
    lower_frame.place(relx=0.5, rely=0.25, relwidth=0.75, relheight=0.6, anchor='n')

    frame = tk.Frame(root, bg='#AFF9A9', bd=5)
    frame.place(relwidth=0.75, relheight=0.1, relx=0.5, rely=0.1, anchor='n')

    button = tk.Button(frame, text='Enter food or ingredient', font=40, command=lambda: 
    test_function(entry.get()))
    button.place(relx=0.55, relwidth=0.45, relheight=1)

    entry = tk.Entry(frame, font=40)
    entry.place(relwidth=0.5, relheight=1)

    listbox = tk.Listbox(lower_frame, justify='left', bd=4)
    listbox.place(relwidth=1, relheight=1)

    label = tk.Label(listbox, anchor='nw', justify='left', bd=4)
    label.place(relwidth=1, relheight=1)

    scrollbar = Scrollbar(listbox, orient="vertical")
    scrollbar.pack( side = 'right', fill = 'y' )

    root.mainloop()

我是用文本框做的,你可以看到它是如何工作的。将其改回您的列表框

import tkinter as tk
from tkinter import Scrollbar

root = tk.Tk()
root.title('Italian-recipes')
height=600
width=895

canvas = tk.Canvas(root, height=height, width=width)
canvas.pack()

lower_frame = tk.Frame(root, bg='#80c1ff', bd=5)
lower_frame.place(relx=0.5, rely=0.25, relwidth=0.75, relheight=0.6, anchor='n')

frame = tk.Frame(root, bg='#AFF9A9', bd=5)
frame.place(relwidth=0.75, relheight=0.1, relx=0.5, rely=0.1, anchor='n')

scrollbar = Scrollbar(lower_frame, orient="vertical")
scrollbar.pack( side = 'right', fill = 'y' )

listbox = tk.Text(lower_frame, yscrollcommand=scrollbar.set,  bd=4)
listbox.pack(fill="both", expand="yes")
scrollbar.configure(command=listbox.yview)


root.mainloop()