tkinter window 在无法调整大小时更改缩放状态下的大小

tkinter window changes size in zoomed state when made unresizeable

我遇到了一个问题,我想要(最初)全屏 window 有时应该可以调整大小,有时则不能。但我发现(在 windows 上)当我使其不可调整大小时,它会更改其大小以填充完整的 window,包括我不希望它执行的任务栏。我希望它保持最初设置为缩放时的大小(很明显)。

可重现的例子:

from tkinter import Tk

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

终于明白了,这是你想要的吗?

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()

在尝试了 jizhihaoSAMA 的解决方案和我自己的想法后,我想到了这个:

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()

我知道它有点笨拙,但它很有魅力。