tkinter绑定鼠标滚轮滚动事件 canvas.bind("<MouseWheel>", processWheel) 失效

其实,不同系统不一样,我的是Ubuntu 所以是<Button-4>   <Button-5>

 

见下:

来自:https://stackoverflow.com/questions/17355902/python-tkinter-binding-mousewheel-to-scrollbar

 

Platform differences:

 

  • On Windows, you bind to <MouseWheel> and you need to divide event.delta by 120 (or some other factor depending on how fast you want the scroll)
  • on OSX, you bind to <MouseWheel> and you need to use event.delta without modification
  • on X11 systems you need to bind to <Button-4> and <Button-5>, and you need to divide event.delta by 120 (or some other factor depending on how fast you want to scroll)

 

转载于:https://www.cnblogs.com/YouXiangLiThon/p/7725308.html

self.root = root self.root.title("图片浏览器(拖动缩放+比例保持)") # 初始化参数 self.image_folder = "pic" self.image_list = sorted([f for f in os.listdir(self.image_folder) if f.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp'))]) self.current_index = 0 self.original_img = None self.tk_image = None self.scale = 1.0 self.min_scale = 0.1 self.max_scale = 5.0 self.canvas_width = 800 self.canvas_height = 600 self.drag_data = {"x": 0, "y": 0, "dragging": False} self.boundary = {"left": 0, "right": 0, "top": 0, "bottom": 0} # 边界限制 self.coord_mode = False self.markers = [] self.current_marker = None # 创建界面 self.canvas = tk.Canvas(self.root, width=self.canvas_width, height=self.canvas_height, bg="black") self.canvas.pack(fill=tk.BOTH, expand=True) btn_frame = tk.Frame(self.root) btn_frame.pack(fill=tk.X, pady=5) tk.Button(btn_frame, text="上一张", command=self.prev_image).pack(side=tk.LEFT) tk.Button(btn_frame, text="下一张", command=self.next_image).pack(side=tk.LEFT) self.coord_btn = tk.Button(btn_frame,text="坐标模式",command=self.toggle_coord_mode) self.coord_btn.pack(side=tk.LEFT) #坐标显示标签 self.coord_label = tk.Label(self.root,text="坐标:(0,0)",fg="white",bg="black") self.coord_label.pack(side=BOTTOM,fill=tk.X) # 绑定事件 self.canvas.bind("<MouseWheel>", self.on_zoom) self.canvas.bind("<ButtonPress-1>", self.on_drag_start) self.canvas.bind("<B1-Motion>", self.on_drag_move) self.canvas.bind("<ButtonRelease-1>", self.on_drag_stop) self.canvas.bind("<Motion>", self.show_coordinates) self.canvas.bind("<ButtonPress-3>", self.add_marker) if self.image_list: self.load_current_image()将长宽改为自适应屏幕大小
最新发布
07-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值