Python Tkinter Custom Widget (Frame) with Mousewheel Scroll Event

Multi tool use
Multi tool use

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












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

#[...]






share|improve this question



























    up vote
    2
    down vote

    favorite












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

    #[...]






    share|improve this question























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











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

      #[...]






      share|improve this question













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

      #[...]








      share|improve this question












      share|improve this question




      share|improve this question








      edited May 25 at 9:18









      Ludisposed

      5,68621656




      5,68621656









      asked May 25 at 8:55









      K-Doe

      533




      533

























          active

          oldest

          votes











          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f195146%2fpython-tkinter-custom-widget-frame-with-mousewheel-scroll-event%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes










           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f195146%2fpython-tkinter-custom-widget-frame-with-mousewheel-scroll-event%23new-answer', 'question_page');

          );

          Post as a guest













































































          pH,Ps0JBpBiv2Os,3o1Yy KfP6L,JXbqX3fgunx2a v,M7Cnuuz ojT S1A7BnK3Wo4,C
          C45eVIkXjZic,xglCccBcOazMYsV n6,PfPjAsZNG1B

          Popular posts from this blog

          Chat program with C++ and SFML

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          Python - Quiz Game with Tkinter