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

2024-05-19 14:00:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用鼠标滚轮在tkinter中向下滚动我的列表。我怎样才能做到这一点

我已经尝试过定义mouseweel函数并将mouseweel绑定到画布框架。但没用

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()

Tags: tkinterupdatecolumnrootgridrowbgsticky