Tkinter .geometry()未调整窗口大小

时间:2019-01-14 07:42:35

标签: python python-3.x tkinter

Tkinter .geometry()函数不会改变窗口的大小,即使我按该按钮时也应该如此,而且我知道该函数正在运行,原因是我要求它打印它正在运行到控制台。这是代码。

import tkinter

class FullScreenWindow():
    def __init__(self):
        self.window = tkinter.Tk()
        self.window.attributes('-zoomed', True)
        self.frame = tkinter.Frame(self.window)
        self.frame.pack()
        self.state = False
        self.window.bind("<F11>", self.toggleFullscreen)
        self.window.bind("<Escape>", self.smallScreen)

    def toggleFullscreen(self, event=None):
        self.state = not self.state
        self.window.attributes("-fullscreen", self.state)

    def smallScreen(self, event=None):
        self.state = False
        self.window.attributes("-fullscreen", self.state)
        print("I am running")
        self.window.geometry("600x525")

fsw = FullScreenWindow()

fsw.window.mainloop()

1 个答案:

答案 0 :(得分:1)

@CommonSense正确。 “缩放”覆盖了我想使用.geometry进行的更改。通过添加

self.window.atrributes('-zoomed', False)

进入smallScreen函数。

这是固定代码

import tkinter

class FullScreenWindow():
    def __init__(self):
        self.window = tkinter.Tk()
        self.window.attributes('-zoomed', True)
        self.frame = tkinter.Frame(self.window)
        self.frame.pack()
        self.state = False
        self.window.bind("<F11>", self.toggleFullscreen)
        self.window.bind("<Escape>", self.smallScreen)

    def toggleFullscreen(self, event=None):
        self.state = not self.state
        self.window.attributes("-fullscreen", self.state)

    def smallScreen(self, event=None):
        self.state = False
        self.window.attributes("-fullscreen", self.state)
        self.window.attrebutes("-zoomed", self.state)
        self.window.geometry("600x525")

fsw = FullScreenWindow()

fsw.window.mainloop()