-1

What I want is to make my computer sleep after about 10 seconds but I want it to have a message with a cancel button

this is what I tried:

this is my warning with 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()  

I need it to count back until sleep (this command):

import time
import os

os.system("Powercfg -H OFF")
os.system("rundll32.exe powrprof.dll,SetSuspendState 0,1,0")

but if I press the cancel button it will stop and nothing will happen

Vini.g.fer
  • 11,639
  • 16
  • 61
  • 90
Squash
  • 955
  • 2
  • 7
  • 17
  • what part of the problem are you struggling with? Do you know about the `after` command? – Bryan Oakley May 14 '16 at 12:21
  • i do know how to do the alert thing (the first one) and i do know how to do the sleep function i need to make the program count down from 10 or something and if it reaches 0 without anyone pressing the cancel button it will go to sleep (the second code) – Squash May 14 '16 at 12:29

1 Answers1

0

You can use after method. after(DELAY_MS, CALLBACK=None, *args). Something like this:

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()  
Stevo Mitric
  • 1,570
  • 18
  • 19
  • thank you so much it works perfectly can i ask you where did you learn python if its a website can yo please give me the link and if its a book can you give me a name thank you anyway! – Squash May 15 '16 at 18:40
  • You're welcome. It's just some small experience. This code is far from perfect, but if it works... – Stevo Mitric May 15 '16 at 19:23
  • hey dude i have a problem i need to wait to ask this question and to be honest i don't really want to wait but i want to make a config file for this script and i just cant get it to work i looked at the tutorial in the python website can you just give me a better one please i am really sorry i just dont know what to do now – Squash May 20 '16 at 16:35
  • I don't understand what you wont to do .? – Stevo Mitric May 20 '16 at 17:30
  • i want to have settings for the script. like if i change somthing in the settings it will shutdown and not sleep every time – Squash May 20 '16 at 18:09