如何使Tkinter窗口不可调整大小?

48

我需要一个使用Tkinter模块创建静态(不可调整大小)窗口的Python脚本。

我有一个相当简单的Tkinter脚本,但我不希望它可以被调整大小。如何防止Tkinter窗口可调整大小?我真的不知道该怎么做。

这是我的脚本:

from tkinter import *
import ctypes, os

def callback():
    active.set(False)
    quitButton.destroy()
    JustGo = Button(root, text=" Keep Going!", command= lambda: KeepGoing())
    JustGo.pack()   
    JustGo.place(x=150, y=110)
    #root.destroy()         # Uncomment this to close the window

def sleep():
    if not active.get(): return
    root.after(1000, sleep)
    timeLeft.set(timeLeft.get()-1)
    timeOutLabel['text'] = "Time Left: " + str(timeLeft.get())  #Update the label
    if timeLeft.get() == 0:                                     #sleep if timeLeft = 0
        os.system("Powercfg -H OFF")
        os.system("rundll32.exe powrprof.dll,SetSuspendState 0,1,0")

def KeepGoing():
    active.set(True)   
    sleep()
    quitButton1 = Button(root, text="do not sleep!", command=callback)
    quitButton1.pack()   
    quitButton1.place(x=150, y=110)

root = Tk()
root.geometry("400x268")
root.title("Alert")
root.configure(background='light blue')

timeLeft = IntVar()
timeLeft.set(10)            # Time in seconds until shutdown

active = BooleanVar()
active.set(True)            # Something to show us that countdown is still going.

label = Label(root, text="ALERT this device will go to sleep soon!",   fg="red")
label.config(font=("Courier", 12))
label.configure(background='light blue')
label.pack()
timeOutLabel = Label(root, text = 'Time left: ' + str(timeLeft.get()),     background='light blue') # Label to show how much time we have left.
timeOutLabel.pack()
quitButton = Button(root, text="do not sleep!", command=callback)
quitButton.pack()   
quitButton.place(x=150, y=110)



root.after(0, sleep)
root.mainloop()  

标题提到了画布,但是在代码中没有使用Canvas小部件。您是在询问如何将整个窗口设置为固定大小吗? - Bryan Oakley
是的,画布的部分是一个错误。我已经编辑了问题,但没有改变标题。抱歉。 - Squash
1个回答

117

在根窗口上使用 resizable 方法需要传入两个布尔值参数,用于描述窗口在X和Y方向是否可调整大小。如果要完全固定窗口大小,将两个参数都设置为False

root.resizable(False, False)

有没有办法使tkinter窗口,顶级我的意思是它不会在桌面背景中单击时隐藏? - Marinos TBH

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接