Nummer_42O
Nummer_42O

Reputation: 432

tkinter window changes size in zoomed state when made unresizeable

I came across the problem that I want to have a (initially) fullscreen window which sometimes should be resizeable and sometimes not. But I found that (on windows) when I make it unresizeable it changes its size to fill the complete window including the taskbar which I don't want it to do. I want it to stay the size it initially was when I have set it zoomed (obviously).

Reproducable example:

from tkinter import Tk

root=Tk()
root.state('zoomed') #until here is everything normal
root.resizable(False,False) #here taskbar gets hidden
root.mainloop()

Upvotes: 1

Views: 2486

Answers (2)

Nummer_42O
Nummer_42O

Reputation: 432

After some playing around with jizhihaoSAMAs solution and my own ideas I came up with this:

from tkinter import *

class App(Tk):
    def __init__(self):
        Tk.__init__(self)
        self.state('zoomed')
        self.update()
        self.maxsize(-1,self.winfo_height())
        self.state('normal')
        self.wip=False
        self.lastgeom=None
        self.bind('<Configure>',self.adopt)
        #without this after disabling and reenabling resizing in zoomed state
        #the window would get back to normal state but not to it's prior size
        #so this behavior is secured here
        self.bind('<x>',self.locksize)
        #enable/disable active resizing
    def adopt(self,event):
        if not self.wip:
            self.wip=True
            if self.state()=='zoomed' and not self.lastgeom:
                self.state('normal')
                self.update()
                self.lastgeom=self.geometry()
                self.state('zoomed')
            elif self.state()=='normal' and self.lastgeom:
                self.geometry(self.lastgeom)
                self.lastgeom=None
            self.wip=False
    def locksize(self,event):
        if self.resizable()[0]: self.resizable(False,False)
        else: self.resizable(True,True)

if __name__=='__main__':
    App().mainloop()

I know it's a bit clunky but it works like a charm.

Upvotes: 2

jizhihaoSAMA
jizhihaoSAMA

Reputation: 12672

Finally,I got this,Is this what you want?

from tkinter import *

def SetSize():
    width, height, X_POS, Y_POS = root.winfo_width(), root.winfo_height(), root.winfo_x(), root.winfo_y()
    root.state('normal')
    root.resizable(0,0)
    root.geometry("%dx%d+%d+%d" % (width, height, X_POS, Y_POS))

root=Tk()
root.state('zoomed') #until here is everything normal
root.after(100,SetSize)
root.mainloop()

Upvotes: 1

Related Questions