2

I am working on a Tkinter GUI and I want to add window sizing and positioning control in its separate class. My structure is like so:

class MainApp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        self.winsize= WinSize(container, 700, 500, 650, 450, True, True)

I want WinSize to set the geometry of the main application as well as handle the minsize, resizeable in the WinSize class.

class WinSize:

    def __init__(self, container, width, height, minx, miny, sizeablex, sizeabley):
        self.container = container
        self.width = width
        self.height = height
        self.minx = minx
        self.miny = miny
        self.sizeablex = sizeablex
        self.sizeabley = sizeabley
        self.ws = self.container.winfo_screenwidth()
        self.hs = self.container.winfo_screenheight()
        self.xpos = (self.ws / 2) - (self.width / 2)
        self.ypos = (self.hs / 2) - (self.height / 2)

I have been searching extensively and found no solution/guidance on how to get those implemented in the WinSize class for that particular instance and/or Frame. I want to use the same class to be able to set size and other attributes to pop-up messages/Frames that will display other information. The main GUI class is taken from @Bryan Oakley's famous example: https://stackoverflow.com/a/7557028/7703610

How do I call the geometry, minsize and resizeable from Tk without having to inherit Tk again in my WinSize class and then apply it to that particular instance?

1 Answer 1

1

There are two solutions. You can pass the window to WinSize() instead of the container, or you can use the winfo_toplevel method to get the window for a particular widget.

The first solution would look like this:

class MainApp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        ...
        self.winsize= WinSize(self, 700, 500, 650, 450, True, True)

class WinSize:

    def __init__(self, window, width, height, minx, miny, sizeablex, sizeabley):
        self.window = window
        ...
        self.window.geometry("%d%d+%d+%d" % (width, height, minx, miny))
        ...

The second solution requires no changes to the main app. Just add the following to WinSize:

class WinSize:
    def __init__(...):
        ...
        window = self.container.winfo_toplevel()
        self.window.geometry("%d%d+%d+%d" % (width, height, minx, miny))
Sign up to request clarification or add additional context in comments.

1 Comment

Thank You @Bryan Oakley. Love how simple you make your solutions. Wish you did a set of video tutorials and teach the rest of us properly. Just a quick question... is self passed into the window variable in the first example?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.