我的滚动条不适用于鼠标的滚动条

My scrollbar is not working with mouse's scroller

我已经重复使用了代码。
我正在尝试滚动此框架并且滚动条正在工作但是 我希望使用鼠标滚轮来滚动它。 我应该怎么办? 我希望它只垂直滚动。

from tkinter import *
root = Tk()
root['bg'] = 'wheat'


frame_container=Frame(root, width = 1000)
frame_container['bg'] = 'wheat'
canvas_container=Canvas(frame_container, width = 1000)
canvas_container['bg'] = 'wheat'


frame2=Frame(canvas_container, width = 1000)
frame2['bg'] = 'wheat'

scrollbar_tk = Scrollbar(frame_container, 
orient="vertical",command=canvas_container.yview)#, 
yscrollcommand=scrollbar_tk.set
 # will be visible if the frame2 is to to big for the canvas
canvas_container.create_window((0,0),window=frame2,anchor='nw')


naan = IntVar()
roti=IntVar()
dal=IntVar()
manchurian = IntVar()
makhani=IntVar()
masala_bhindi = IntVar()
chole = IntVar()
rajma = IntVar()
shahi_panneer = IntVar()
kadahi_paneer = IntVar()
masala_gobhi = IntVar()
allo_gobhi = IntVar()
matar_paneer = IntVar()
menu_roti = "Tava Roti             25 ₹/piece"
menu_dal = "Dal                       80 ₹/bowl"
menu_makhani = "Dal Makhni          110 ₹/bowl"
menu_naan = "Naan                    50 ₹/piece"
menu_manchurian = "Manchurian         110 ₹/plate"     
menu_shahi_panneer = "Shahi paneer      110₹/bowl"
menu_kadahi_paneer = "Kadhai paneer     150/bowl"
menu_masala_gobhi = "Masala gobhi      130₹/bowl"
menu_allo_gobhi = "Aloo gobhi          120₹/bowl" 
menu_matar_paneer = "Matar paneer      135₹/bowl"
menu_masala_bhindi = "Masala bhindi     110₹/bowl"
menu_chole = "Chole                 100₹/bowl"  
menu_rajma = "Rajama              150₹/bowl"
menu_chaap = "Chaap                125₹/bowl"
menu_aloo_parntha = "Aloo parantha     35₹/peice" 
menu_cheele = "Cheele                55₹/peice "

listItems = [menu_roti,menu_dal,menu_makhani, menu_naan, 
menu_manchurian, menu_shahi_panneer,
             menu_kadahi_paneer, menu_masala_gobhi, 
menu_allo_gobhi, menu_matar_paneer, menu_masala_bhindi, 
menu_chole, menu_rajma, menu_chaap, menu_aloo_parntha, 
 menu_cheele]


Title = Label(frame2, text = "                Food Items         
Prices         Quantities", fg = 'red', bg = 'wheat', font= 
("arial", 30))
Title.grid()
for item in listItems:
    label = Label(frame2,text=item, fg = 'yellow', bg = 
'wheat', font=("arial", 30))
    label.grid(column=0, row=listItems.index(item)+1)


q_roti = Entry(frame2, font=("arial",20), textvariable = roti, 
fg="Black", width=10)
q_roti.grid(column = 1, row = 1)
q_dal = Entry(frame2, font=("arial",20), textvariable = dal, 
fg="black", width=10)
q_dal.grid(column = 1, row = 2)
q_makhani = Entry(frame2, font=("arial",20), textvariable = 
makhani, fg="black", width=10)
q_makhani.grid(column = 1, row = 3)
q_naan = Entry(frame2, font=("arial",20), textvariable = naan, 
fg="black", width=10)
q_naan.grid(column = 1, row = 4)
q_manchurian = Entry(frame2,font=("arial",20), textvariable = 
manchurian, fg="black", width=10)
q_manchurian.grid(column = 1, row = 5)
q_shahi_panneer = Entry(frame2, font=("arial",20), textvariable 
= shahi_panneer, fg="black", width=10)
q_shahi_panneer.grid(column = 1, row = 6)
q_kadahi_panneer = Entry(frame2, font=("arial",20), 
textvariable = kadahi_paneer, fg="black", width=10)
q_kadahi_panneer.grid(column = 1, row = 7)
q_masala_gobhi = Entry(frame2, font=("arial",20), textvariable 
= masala_gobhi, fg="black", width=10)
q_masala_gobhi.grid(column = 1, row = 8)
q_allo_gobhi = Entry(frame2, font=("arial",20), textvariable = 
allo_gobhi, fg="black", width=10)
q_allo_gobhi.grid(column = 1, row = 9)
q_matar_panneer = Entry(frame2, font=("arial",20), textvariable 
= matar_paneer, fg="black", width=10)
q_matar_panneer.grid(column = 1, row = 10)
q_masala_bhindi = Entry(frame2, font=("arial",20), textvariable 
= masala_bhindi, fg="black", width=10)
q_masala_bhindi.grid(column = 1, row = 11)
q_cholle = Entry(frame2,font=("arial",20), textvariable = 
chole, fg="black", width=10)
q_cholle.grid(column = 1, row = 12)
q_rajma = Entry(frame2,font=("arial",20), textvariable = rajma, 
fg="black", width=10)
q_rajma.grid(column = 1, row = 13)

frame2.update() # update frame2 height so it's no longer 0 ( 
height is 0 when it has just been created )
canvas_container.configure(yscrollcommand=scrollbar_tk.set, 
scrollregion="0 0 0 %s" % frame2.winfo_height()) # the 
scrollregion 
mustbe the size of the frame inside it,
                                                                                                            
#in this case "x=0 y=0 width=0 height=frame2height"
                                                                                                            
#width 0 because we only scroll verticaly so don't mind about 
the width.
canvas_container.grid(column = 1, row = 0)

scrollbar_tk.grid(column = 0, row = 0, sticky='ns')


frame_container.grid()#.pack(expand=True, fill='both')

root.mainloop()

对不起这个代码。这不是很容易理解,但对于我这个级别的人来说可能就足够了。请有人给我一些建议来提高我的技能。

您可以使用 <MouseWheel> 虚拟事件来滚动 canvas 并最终滚动框架。

canvas_container.create_window((0,0),window=frame2,anchor='nw')
def _on_mousewheel(event):
    canvas_container.yview_scroll(-1*int(event.delta/120), "units")   
canvas_container.bind_all("<MouseWheel>", _on_mousewheel)