'Passing non-default arguments to Tk class constructor

I want to pass two Queues objects to a class the inherits Tk.tk.

main.py code:

qin = Queue()
qout = Queue()

gui = GUI(qin,qout)
gui.mainloop()

gui.py code:

class GUI(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs) #if im calling tk.Tk.__init__(self) nothing is displayed to the screen so *args, **kwargs is requirement for some reason, not sure why

        self.qin = args[0]
        self.qout = args[1]

    ...

error:

    gui = GUI(qin,qout)
  File "/home/a/gui.py", line 20, in __init__
    tk.Tk.__init__(self, *args, **kwargs)
  File "/usr/lib/python3.9/tkinter/__init__.py", line 2270, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
TypeError: create() argument 1 must be str or None, not Queue

How to fix this error?

Thanks.



Solution 1:[1]

Just pop the args off of kwargs before passing kwargs to the superclass init.

class GUI(tk.Tk):

    def __init__(self, *args, **kwargs):
        qin = kwargs.pop("qin", None)
        qout = kwargs.pop("qout", None)
        tk.Tk.__init__(self, *args, **kwargs)

Or, if you don't care about the caller being able to pass in other arguments through to the superlcass, just define them as normal arguments:

 class GUI(tk.Tk):

    def __init__(self, qin, qout):
        tk.Tk.__init__(self)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bryan Oakley