2
\$\begingroup\$

I try to write a GUI for practicing with one Window, containing two frames in a ttk.Notebook. Each of these Custom Widget Frames should have a scrollbar and the MouseWheel bound to a yscroll for the canvas which is placed in this frame.

My questions are: - Is this a common/recommended way to do that? - If I do it like this the MouseWheel works only on one of the Frames of my window. Why? - I am a beginner, so if you see any other mistakes/uncommon things, please let me know!

Here is an extraction of my code:

from tkinter import *
import tkinter.ttk as ttk

class MyFirstGUI(object):
    def __init__(self, master):
        self.master = master

        self.gui_control = ttk.Notebook(master)

        self.main_gui = ttk.Frame(self.gui_control, borderwidth=0) 
        self.systems_gui = ttk.Frame(self.gui_control, borderwidth=0)

        self.gui_control.add(self.main_gui, text='Main') 
        self.gui_control.add(self.systems_gui, text='Systems')

        self.gui_control.grid(column=1,row=1, columnspan=9, rowspan=50,sticky=N+E+S+W)  

        self.test_1 = Main.MainFrame(self.main_gui)
        self.test_1.grid(row=0, column=0, sticky="NESW") 

        self.test_2 = Systems.SystemFrame(self.systems_gui)
        self.test_2.grid(row=0, column=0, sticky="NESW") 

        #[...]

master.mainloop()

MainGUI:

class MainFrame(tk.Frame):    
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)        

        self.main_canvas = tk.Canvas(self, borderwidth=0, highlightthickness=0)
        self.main_canvas.grid(row=0,column=0, sticky="NESW")  

        self.vsb = tk.Scrollbar(self, orient="vertical", command=self.main_canvas.yview)
        self.vsb.grid(row=0,column=1, sticky="NS") 

        self.main_canvas.bind_all("<MouseWheel>", self._on_mousewheel)

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

    #[...]

Systems GUI (basically the same):

class SystemFrame(tk.Frame):    
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)        

        self.system_canvas = tk.Canvas(self, borderwidth=0, highlightthickness=0)
        self.system_canvas.grid(row=0,column=0, sticky="NESW")  

        self.vsb = tk.Scrollbar(self, orient="vertical", command=self.system_canvas.yview)
        self.vsb.grid(row=0,column=1, sticky="NS") 

        self.system_canvas.bind_all("<MouseWheel>", self._on_mousewheel)

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

    #[...]
\$\endgroup\$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.