【问题标题】:How to make a Tkinter window not resizable?如何使 Tkinter 窗口无法调整大小?
【发布时间】:2016-09-23 14:57:09
【问题描述】:

我需要一个使用 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 小部件。您是在询问将整个窗口设置为固定大小吗?
  • 是的,画布的东西是一个错误,我编辑了这个问题,但没有改变标题抱歉

标签: python python-3.x tkinter static


【解决方案1】:

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

root.resizable(False, False)

【讨论】:

  • 谢谢,这对我有很大帮助。有没有办法删除退出 (x) 按钮等等?
  • @IDKanything:搜索此站点。这个问题已经被问了好几次了。
猜你喜欢
  • 2014-03-24
  • 1970-01-01
  • 2016-05-22
  • 1970-01-01
  • 1970-01-01
  • 2011-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多