pythontkintermousewheel

TKinter mousewheel scroll on multipe tabs


I can't seem to get the mousewheel to scroll on a multiple tab TKinter. It only scrolls the first Tab.

Tried a few different things but cannot seem to work it out. Any help would be great :)

Basiclly I just want the current tab to scroll on mousewheel.

Here is the basis on my code:

import tkinter as tk
from tkinter import ttk



root = tk.Tk()

notebook = ttk.Notebook(root)
notebook.pack(pady=10, expand=True)

tab1 = tk.Frame(notebook, width=1200, height=600)
tab2 = tk.Frame(notebook, width=1200, height=600)
tab3 = tk.Frame(notebook, width=1200, height=600)

tab1.pack(fill='both', expand=1)
tab2.pack(fill='both', expand=1)
tab3.pack(fill='both', expand=1)

notebook.add(tab1, text='Tab 1')
notebook.add(tab2, text='Tab 2')
notebook.add(tab3, text='Tab 3')


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


######################## TAB 1 ########################
outerFrame1 = ttk.Frame(tab1)
outerFrame1.pack(side="left", fill="both", expand = True)

canvas1 = tk.Canvas(outerFrame1, width=1180, height=600)

scrollbar1 = ttk.Scrollbar(outerFrame1, orient='vertical',command=canvas1.yview)
scrollbar1.pack(side="right",fill="y")

canvas1.pack(side="left", fill="both", expand = True)

canvas1.configure(scrollregion=canvas1.bbox('all'), yscrollcommand=scrollbar1.set)
canvas1.bind('<Configure>', lambda e: canvas1.configure(scrollregion = canvas1.bbox("all")))
canvas1.bind_all("<MouseWheel>", _on_mousewheel)

frame1 = ttk.Frame(canvas1)
canvas1.create_window((0,0),window=frame1,anchor="n")


######################## TAB 2 ########################
outerFrame2 = tk.Frame(tab2)
outerFrame2.pack(fill="both", expand=1)

canvas2 = tk.Canvas(outerFrame2, width=1180, height=600)

scrollbar2 = ttk.Scrollbar(outerFrame2, orient='vertical',command=canvas2.yview)
scrollbar2.pack(side="right",fill="y")

canvas2.pack(side="left", fill="both", expand = True)

canvas2.configure(scrollregion=canvas1.bbox('all'), yscrollcommand=scrollbar2.set)
canvas2.bind('<Configure>', lambda e: canvas2.configure(scrollregion = canvas2.bbox("all")))
canvas2.bind_all("<MouseWheel>", _on_mousewheel)

frame2 = ttk.Frame(canvas2)
canvas2.create_window((0,0),window=frame2,anchor="n")



root.mainloop()

Solution

  • In the function add all the scrollable widgets

    def _on_mousewheel(event):
    
        #you can add more widgets inside the list separated by comma  
        for x in [canvas1, canvas2]:
            x.yview_scroll(int(-1*(event.delta/120)), "units")