簡體   English   中英

如何使 Tkinter 窗口無法調整大小?

[英]How to make a Tkinter window not resizable?

我需要一個使用 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()  

根窗口上的resizable方法采用兩個布爾參數來描述窗口是否在 X 和 Y 方向上可調整大小。 要使其大小完全固定,請將兩個參數設置為False

root.resizable(False, False)

您還可以使用:

root.resizable(0, 0)

因為在Python 3中0表示False。

我需要一個使用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()  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM