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

我有一个非常简单的Tkinter脚本,但我不希望它可以调整大小.如何防止Tkinter窗口的大小调整?我真的不知道该怎么办.

这是我的 playbook :

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

推荐答案

根窗口上的resizable方法使用两个布尔参数来描述窗口在X和Y方向是否可调整大小.要使其大小完全固定,请将两个参数都设置为False:

root.resizable(False, False)

Python-3.x相关问答推荐

如何将CSV或FDF数据解析到Python词典并注入到模板PDF表单中?

tkinter treeview 如何在获取所选项目时将设置的对象作为对象返回

如何使用正则表达式通过反向搜索从链接中获取特定文本

如何将值映射到具有上限和下限的新列

PyQt5 中耦合滑块和拨号小部件.解决结果不一致的问题

Django - ValueError:无法将字符串转换为浮点数:''

在 Python 中实现 COM 接口

如何在 Telethon 中向机器人发送发送表情符号

通过 requests 库调用 API 获取访问令牌

Tkinter IntVar 返回 PY_VAR0 而不是值

Django 2 个字段之一不能为空

预分配一个无列表

numpy.ndarray 与 pandas.DataFrame

virtualenv virtualenvwrapper virtualenv:错误:无法识别的参数:--no-site-packages

Python图例属性错误

Selenium (Python) - 使用 Chrome 网络驱动程序等待下载过程完成

在python中,如果一个函数没有return语句,它会返回什么?

Pruning in Keras

为什么排序列表比未排序列表大

注册 Celery 基于类的任务