如何在tkinter中使用鼠标滚轮滚动

时间:2019-11-27 11:54:05

标签: python tkinter scrollbar bind mousewheel

我希望能够使用鼠标滚轮在tkinter中向下滚动列表。我该如何实现?

我已经尝试定义鼠标滚轮功能并将鼠标滚轮绑定到canvasframe。但这没用。

from tkinter import *
def update_scrollregion(event):
    photoCanvas.configure(scrollregion=photoCanvas.bbox("all"))

root = Tk()
root.geometry("1700x900")   

photoFrame = Frame(root, width=250, height=190, bg="#EBEBEB")
photoFrame.grid()
photoFrame.rowconfigure(0, weight=1) 
photoFrame.columnconfigure(0, weight=1) 

photoCanvas = Canvas(photoFrame, bg="#EBEBEB")
photoCanvas.grid(row=0, column=0, sticky="nsew")

canvasFrame = Frame(photoCanvas, bg="#EBEBEB")
photoCanvas.create_window(0, 0, window=canvasFrame, anchor='nw')

i = 1
while i < 77:
  Label(canvasFrame,text="example",padx=15).grid(row=i,column=1,sticky="W")
  i += 1

photoScroll = Scrollbar(photoFrame, orient=VERTICAL)
photoScroll.config(command=photoCanvas.yview)
photoCanvas.config(yscrollcommand=photoScroll.set)
photoScroll.grid(row=0, column=1, sticky="ns")

canvasFrame.bind("<Configure>", update_scrollregion)

root.mainloop()

0 个答案:

没有答案