0

I have this code working properly on Windows :

from tkinter import *
from tkinter import ttk


def _on_mouse_wheel(event):
    my_main_canvas.yview_scroll(-1 * int((event.delta / 120)), "units")


window = Tk()
main_frame = Frame(window)
main_frame.pack(fill=BOTH, expand=YES)
my_main_canvas = Canvas(main_frame)
my_main_canvas.pack(side=LEFT, fill=BOTH, expand=YES)
my_main_scrollbar = ttk.Scrollbar(main_frame, orient=VERTICAL, command=my_main_canvas.yview)
my_main_scrollbar.pack(side=RIGHT, fill=Y)
my_main_canvas.configure(yscrollcommand=my_main_scrollbar.set)
my_main_canvas.bind('<Configure>', lambda e: my_main_canvas.configure(scrollregion = my_main_canvas.bbox("all")))
my_main_canvas.bind_all("<MouseWheel>", _on_mouse_wheel)
new_main_frame = Frame(my_main_canvas, relief=SUNKEN)
my_main_canvas.create_window((0,0), window=new_main_frame, anchor="center", width=window.winfo_screenwidth()-30)
for i in range(100):
    Label(new_main_frame, text=f"Label {i+1}").pack()
window.mainloop()

I would like to do the Linux version of it. My problem is that I can't scroll with the mouse wheel.

I have found that the bidings must be with <Button-4> and <Button-5> instead of <MouseWheel>, and the _on_mouse_wheel(event) function may need modifications but I couldn't figured out.

Val
  • 1

0 Answers0