Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在一段时间后使用“取消”按钮执行命令_Python_Tkinter_Sleep_Sleep Mode - Fatal编程技术网

Python 如何在一段时间后使用“取消”按钮执行命令

Python 如何在一段时间后使用“取消”按钮执行命令,python,tkinter,sleep,sleep-mode,Python,Tkinter,Sleep,Sleep Mode,我想要的是让我的电脑在大约10秒钟后进入睡眠状态,但我希望它有一条带有取消按钮的消息 这就是我所尝试的: 这是我对tkinter的警告: from tkinter import * import ctypes def callback(): quit() root = Tk() root.geometry("400x268") root.title("Alert") root.configure(background='light blue') label = Label(root,

我想要的是让我的电脑在大约10秒钟后进入睡眠状态,但我希望它有一条带有取消按钮的消息

这就是我所尝试的:

这是我对tkinter的警告:

from tkinter import *
import ctypes

def callback():
quit()


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



label = Label(root, text="ALERT this device will go to sleep soon!",   fg="red")
label.config(font=("Courier", 12))
label.configure(background='light blue')
quitButton = Button(root, text="do not sleep!", command=callback)
quitButton.pack()
quitButton.place(x=150, y=150)


label.pack()
root.mainloop()  
我需要它倒数直到睡眠(此命令):


但如果我按下取消按钮,它将停止,并且不会发生任何事情

您可以使用
after
方法
after(DELAY\u MS,CALLBACK=None,*args)
。 大概是这样的:

from tkinter import *
import ctypes, os

def callback():
    active.set(False)
    #root.destroy()         # Uncoment 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")
        callback()

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=150)      

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

你在努力解决问题的哪一部分?你知道
after
命令吗?我知道如何发出警报(第一个),我也知道如何执行睡眠功能,我需要让程序从10开始倒计时,如果没有人按下取消按钮就达到0,它将进入睡眠(第二个代码)非常感谢,它工作得非常完美。我可以问你在哪里学的python吗?如果是一个网站,你可以给我链接,如果是一本书,你可以给我一个名字吗?谢谢!不客气。这只是一些小小的经历。这个代码还远远不够完美,但是如果它能工作…嘿,伙计,我有一个问题,我需要等一下问这个问题,老实说,我真的不想等,但我想为这个脚本做一个配置文件,我只是无法让它工作我看了python网站上的教程,你能不能给我一个更好的,请我真的很抱歉,我现在不知道该怎么做我不明白你不想做什么。?我想对脚本进行设置。比如,如果我在设置中更改某些内容,它将关闭,而不是每次都睡眠
from tkinter import *
import ctypes, os

def callback():
    active.set(False)
    #root.destroy()         # Uncoment 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")
        callback()

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=150)      

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