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?

Ernie Peters
  • 537
  • 1
  • 6
  • 18

1 Answers1

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))
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • 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? – Ernie Peters Jul 07 '17 at 14:38