Skip to main content
remove extra parens in poll function
Source Link
clutton
  • 839
  • 8
  • 13

In looking at your code, there are a few things I would do:

  1. Subclass TK.. class MainGUI(Tk):
  2. Attach all gui components to this class.... your frame for example.
  3. Add a queue and a polling method for GUI updates to the MainGUI class (this may help fix your freezing issue). You can use the TK.after method to ask the mainloop to do this periodically.
  4. Do the heavy lifting items such as db reads, filesystem, etc in a separate thread if they block too long, but make sure they don't update the GUI directly from that thread, you can pass a reference to the GUI to them and they should place a callback on the updateq for the main loop to execute as it runs a polling cycle.
  5. Check the code for pep8 compliance.

Here is an example of how I have done GUI's using TKinter:

class MainGUI(Tk):
    """ The GUI """

    def __init__(self):
        super().__init__()
        self.updateq = queue.Queue()

        # Build main components

        # Layout components

        # Key bindings

        self.poll()
        self.mainloop()

    def poll(self):
        """ Polls the Queue for the arrival of various callbacks """
        # You may want to process more or less callbacks each cycle...
        # here it uses 100 as a maximum
        for i in range(100):
            if( self.updateq.empty()):
                break

            (callback, args) = self.updateq.get()
            if( args):
                callback(args)
            else:
                callback()

        # Recursive call
        self.after(50, self.poll)

In looking at your code, there are a few things I would do:

  1. Subclass TK.. class MainGUI(Tk):
  2. Attach all gui components to this class.... your frame for example.
  3. Add a queue and a polling method for GUI updates to the MainGUI class (this may help fix your freezing issue). You can use the TK.after method to ask the mainloop to do this periodically.
  4. Do the heavy lifting items such as db reads, filesystem, etc in a separate thread if they block too long, but make sure they don't update the GUI directly from that thread, you can pass a reference to the GUI to them and they should place a callback on the updateq for the main loop to execute as it runs a polling cycle.
  5. Check the code for pep8 compliance.

Here is an example of how I have done GUI's using TKinter:

class MainGUI(Tk):
    """ The GUI """

    def __init__(self):
        super().__init__()
        self.updateq = queue.Queue()

        # Build main components

        # Layout components

        # Key bindings

        self.poll()
        self.mainloop()

    def poll(self):
        """ Polls the Queue for the arrival of various callbacks """
        # You may want to process more or less callbacks each cycle...
        # here it uses 100 as a maximum
        for i in range(100):
            if(self.updateq.empty()):
                break

            (callback, args) = self.updateq.get()
            if(args):
                callback(args)
            else:
                callback()

        # Recursive call
        self.after(50, self.poll)

In looking at your code, there are a few things I would do:

  1. Subclass TK.. class MainGUI(Tk):
  2. Attach all gui components to this class.... your frame for example.
  3. Add a queue and a polling method for GUI updates to the MainGUI class (this may help fix your freezing issue). You can use the TK.after method to ask the mainloop to do this periodically.
  4. Do the heavy lifting items such as db reads, filesystem, etc in a separate thread if they block too long, but make sure they don't update the GUI directly from that thread, you can pass a reference to the GUI to them and they should place a callback on the updateq for the main loop to execute as it runs a polling cycle.
  5. Check the code for pep8 compliance.

Here is an example of how I have done GUI's using TKinter:

class MainGUI(Tk):
    """ The GUI """

    def __init__(self):
        super().__init__()
        self.updateq = queue.Queue()

        # Build main components

        # Layout components

        # Key bindings

        self.poll()
        self.mainloop()

    def poll(self):
        """ Polls the Queue for the arrival of various callbacks """
        # You may want to process more or less callbacks each cycle...
        # here it uses 100 as a maximum
        for i in range(100):
            if self.updateq.empty():
                break

            callback, args = self.updateq.get()
            if args:
                callback(args)
            else:
                callback()

        # Recursive call
        self.after(50, self.poll)
added 121 characters in body
Source Link
clutton
  • 839
  • 8
  • 13

In looking at your code, there are a few things I would do:

  1. Subclass TK.. class MainGUI(Tk):
  2. Attach all gui components to this class,.... your frame for example.
  3. Add a queue and a polling method for GUI updates to the MainGUI class (this may help fix your freezing issue). You can use the TK.after method to ask the mainloop to do this periodically.
  4. Do the heavy lifting items such as db reads, filesystem, etc in a separate thread if they block too long, but make sure they don't update the GUI directly from that thread, you can pass a reference to the GUI to them and they should place a callback on the updateq for the main loop to execute as it runs a polling cycle.
  5. Check the code for pep8 compliance.

Here is an example of how I have done GUI's using TKinter:

class MainGUI(Tk):
    """ The GUI """

    def __init__(self):
        super().__init__()
        self.updateq = queue.Queue()

        # Build main components

        # Layout components

        # Key bindings

        self.poll()
        self.mainloop()

    def poll(self):
        """ Polls the Queue for the arrival of various callbacks """
        # You may want to process more or less callbacks each cycle...
        # here it uses 100 as a maximum
        for i in range(100):
            if(self.updateq.empty()):
                break

            (callback, args) = self.updateq.get()
            if(args):
                callback(args)
            else:
                callback()

        # Recursive call
        self.after(50, self.poll)

In looking at your code, there are a few things I would do:

  1. Subclass TK.. class MainGUI(Tk):
  2. Attach all gui components to this class, your frame for example.
  3. Add a queue and a polling method for GUI updates to the MainGUI class (this may help fix your freezing issue).
  4. Do the heavy lifting items such as db reads, filesystem, etc in a separate thread, but make sure they don't update the GUI directly, you can pass a reference to the GUI to them and they should place a callback on the updateq for the main loop to execute as it runs a polling cycle.
  5. Check the code for pep8 compliance.

Here is an example of how I have done GUI's using TKinter:

class MainGUI(Tk):
    """ The GUI """

    def __init__(self):
        super().__init__()
        self.updateq = queue.Queue()

        # Build main components

        # Layout components

        # Key bindings

        self.poll()
        self.mainloop()

    def poll(self):
        """ Polls the Queue for the arrival of various callbacks """
        # You may want to process more or less callbacks each cycle...
        # here it uses 100 as a maximum
        for i in range(100):
            if(self.updateq.empty()):
                break

            (callback, args) = self.updateq.get()
            if(args):
                callback(args)
            else:
                callback()

        # Recursive call
        self.after(50, self.poll)

In looking at your code, there are a few things I would do:

  1. Subclass TK.. class MainGUI(Tk):
  2. Attach all gui components to this class.... your frame for example.
  3. Add a queue and a polling method for GUI updates to the MainGUI class (this may help fix your freezing issue). You can use the TK.after method to ask the mainloop to do this periodically.
  4. Do the heavy lifting items such as db reads, filesystem, etc in a separate thread if they block too long, but make sure they don't update the GUI directly from that thread, you can pass a reference to the GUI to them and they should place a callback on the updateq for the main loop to execute as it runs a polling cycle.
  5. Check the code for pep8 compliance.

Here is an example of how I have done GUI's using TKinter:

class MainGUI(Tk):
    """ The GUI """

    def __init__(self):
        super().__init__()
        self.updateq = queue.Queue()

        # Build main components

        # Layout components

        # Key bindings

        self.poll()
        self.mainloop()

    def poll(self):
        """ Polls the Queue for the arrival of various callbacks """
        # You may want to process more or less callbacks each cycle...
        # here it uses 100 as a maximum
        for i in range(100):
            if(self.updateq.empty()):
                break

            (callback, args) = self.updateq.get()
            if(args):
                callback(args)
            else:
                callback()

        # Recursive call
        self.after(50, self.poll)
Source Link
clutton
  • 839
  • 8
  • 13

In looking at your code, there are a few things I would do:

  1. Subclass TK.. class MainGUI(Tk):
  2. Attach all gui components to this class, your frame for example.
  3. Add a queue and a polling method for GUI updates to the MainGUI class (this may help fix your freezing issue).
  4. Do the heavy lifting items such as db reads, filesystem, etc in a separate thread, but make sure they don't update the GUI directly, you can pass a reference to the GUI to them and they should place a callback on the updateq for the main loop to execute as it runs a polling cycle.
  5. Check the code for pep8 compliance.

Here is an example of how I have done GUI's using TKinter:

class MainGUI(Tk):
    """ The GUI """

    def __init__(self):
        super().__init__()
        self.updateq = queue.Queue()

        # Build main components

        # Layout components

        # Key bindings

        self.poll()
        self.mainloop()

    def poll(self):
        """ Polls the Queue for the arrival of various callbacks """
        # You may want to process more or less callbacks each cycle...
        # here it uses 100 as a maximum
        for i in range(100):
            if(self.updateq.empty()):
                break

            (callback, args) = self.updateq.get()
            if(args):
                callback(args)
            else:
                callback()

        # Recursive call
        self.after(50, self.poll)