【问题标题】:tkinter window changes size in zoomed state when made unresizeable当无法调整大小时,tkinter 窗口会在缩放状态下更改大小
【发布时间】:2020-03-15 14:37:29
【问题描述】:

我遇到了一个问题,我想要一个(最初)全屏窗口,有时应该可以调整大小,有时不能。但是我发现(在 Windows 上)当我使它不可调整大小时,它会改变它的大小以填充整个窗口,包括我不希望它做的任务栏。我希望它保持最初设置为缩放时的大小(显然)。

  • 操作系统:Windows 10 家庭版
  • Python:3.7
  • Tk/Tcl:8.6

可重现的例子:

from tkinter import Tk

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

【问题讨论】:

  • emmm,你的意思是要显示任务栏。对吧?
  • 对。通常在缩放状态下会显示任务栏,但如果 resizable 设置为 False,False,则不会显示。

标签: python-3.x tkinter fullscreen resizable


【解决方案1】:

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

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

【讨论】:

  • 是的,这有帮助,谢谢。尽管通过winfo_y 获取y 坐标会导致窗口在屏幕顶部达到峰值。对我来说,它通过用文字 0 替换对 y 位置的请求来工作。但我不知道这是否适用于每个操作系统甚至 Windows 版本。我很确定它不是。
  • @Nummer_42O 祝你好运。
  • 我对你的解决方案不太满意,所以我又玩了一些,想出了一些目前还不是完美无缺但以更“自然”的方式工作的东西。将其作为另一个答案发布。
【解决方案2】:

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

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

我知道它有点笨拙,但它就像一个魅力。

【讨论】:

    猜你喜欢
    • 2013-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-07
    • 2021-08-18
    • 2013-09-17
    • 2020-09-09
    • 2011-05-18
    相关资源
    最近更新 更多