Estoy haciendo un programa para programar un apagado del sistema. Y entonces, hice estas variables para diferenciar en que formato el usuario quiere introducir su numero, si es en segundos, minutos, horas o dias
inSeconds = False
inMinutes = False
inHours = False
inDays = False
Luego de eso cree un par de funciones para establecer que formato usara el usuario
def setInSeconds():
inSeconds = True
inMinutes = False
inHours = False
inDays = False
def setInMinutes():
inSeconds = False
inMinutes = True
inHours = False
inDays = False
def setInHours():
inSeconds = False
inMinutes = False
inHours = True
inDays = False
def setInDays():
inSeconds = False
inMinutes = False
inHours = False
inDays = True
Y entonces, cree unos botones para establecerlos (no voy a poner todos los codigos porque es innecesario, solo pongo uno ya que todos son iguales)
Por ejemplo, esto es en segundos:
programInSecond = Button(window, text="Segundos", command=setInSeconds)
Puse tambien en las funciones, que imprima en la consola si las variables estan en True o False
Luego de eso hice un boton llamado start
que inicie la cuenta regresiva, la funcion del boton para iniciarla es esta:
def start():
print()
print(inSeconds)
print(inMinutes)
print(inHours)
print(inDays)
if (inSeconds == True):
time = 10
while time > 0:
print("FUNCIONA")
time -= 1
sleep(1)
else:
print("SHUTDOWN")
subprocess.run("shutdown -r")
Tambien (soy mas o menos nuevo en Python, llevo 3 meses) no funciona el metodo del if, while y else, pero eso quiza pueda solucionarlo por mi cuenta, pero si igualmente alguien quiere ayudarme agradezco su ayuda de mas ;)
Mi problema es que por alguna razon, las variables no se ¿"guardan"? (no se como se diria) en su nuevo valor declarado por las funciones setIn<formato>
¿Como hago para que se guarden?
Perdon si quiza lo escribi o exprese mal ciertas cosas, pueden preguntar lo que sea, pero ayuda porfavor
Gracias de antemano ;D
EDIT: Dejo el codigo por completo por las dudas:
# Importando librerias #
from tkinter import *
from time import *
import os
import subprocess
# Vars #
global inSeconds
inSeconds = False
global inMinutes
inMinutes = False
global inHours
inHours = False
global inDays
inDays = False
# Funciones #
def setInSeconds():
inSeconds = True
inMinutes = False
inHours = False
inDays = False
print()
print(inSeconds)
print(inMinutes)
print(inHours)
print(inDays)
def setInMinutes():
inSeconds = False
inMinutes = True
inHours = False
inDays = False
print()
print(inSeconds)
print(inMinutes)
print(inHours)
print(inDays)
def setInHours():
inSeconds = False
inMinutes = False
inHours = True
inDays = False
print()
print(inSeconds)
print(inMinutes)
print(inHours)
print(inDays)
def setInDays():
inSeconds = False
inMinutes = False
inHours = False
inDays = True
print()
print(inSeconds)
print(inMinutes)
print(inHours)
print(inDays)
def start():
print()
print(inSeconds)
print(inMinutes)
print(inHours)
print(inDays)
if (inSeconds == True):
time = 10
while time > 0:
print("FUNCIONA")
time -= 1
sleep(1)
else:
print("SHUTDOWN")
subprocess.run("shutdown -r")
if (inMinutes == True):
if (time == 1):
time = 60
while (time > 0):
time -= 1
sleep(1)
if (time <= 0):
os.system("shutdown /r /t 0")
if (time == 2):
time = 120
while (time > 0):
time -= 1
sleep(1)
if (time <= 0):
os.system("shutdown /r /t 0")
if (time == 3):
time = 180
while (time > 0):
time -= 1
sleep(1)
if (time <= 0):
os.system("shutdown /r /t 0")
if (time == 4):
time = 240
while (time > 0):
time -= 1
sleep(1)
if (time <= 0):
os.system("shutdown /r /t 0")
if (time == 5):
time = 300
while (time > 0):
time -= 1
sleep(1)
if (time <= 0):
os.system("shutdown /r /t 0")
if (time == 6):
time = 360
while (time > 0):
time -= 1
sleep(1)
if (time <= 0):
os.system("shutdown /r /t 0")
if (time == 7):
time = 420
while (time > 0):
time -= 1
sleep(1)
if (time <= 0):
os.system("shutdown /r /t 0")
if (time == 8):
time = 480
while (time > 0):
time -= 1
sleep(1)
if (time <= 0):
os.system("shutdown /r /t 0")
if (time == 9):
time = 540
while (time > 0):
time -= 1
sleep(1)
if (time <= 0):
os.system("shutdown /r /t 0")
if (time == 10):
time = 600
while (time > 0):
time -= 1
sleep(1)
if (time <= 0):
os.system("shutdown /r /t 0")
if (time == 11):
time = 660
while (time > 0):
time -= 1
sleep(1)
if (time <= 0):
os.system("shutdown /r /t 0")
if (time == 12):
time = 720
while (time > 0):
time -= 1
sleep(1)
if (time <= 0):
os.system("shutdown /r /t 0")
if (time == 13):
time = 780
while (time > 0):
time -= 1
sleep(1)
if (time <= 0):
os.system("shutdown /r /t 0")
if (time == 14):
time = 840
while (time > 0):
time -= 1
sleep(1)
if (time <= 0):
os.system("shutdown /r /t 0")
if (time == 15):
time = 900
while (time > 0):
time -= 1
sleep(1)
if (time <= 0):
os.system("shutdown /r /t 0")
if (time == 16):
time = 960
while (time > 0):
time -= 1
sleep(1)
if (time <= 0):
os.system("shutdown /r /t 0")
if (time == 17):
time = 1020
while (time > 0):
time -= 1
sleep(1)
if (time <= 0):
os.system("shutdown /r /t 0")
if (time == 18):
time = 1080
while (time > 0):
time -= 1
sleep(1)
if (time <= 0):
os.system("shutdown /r /t 0")
if (time == 19):
time = 1140
while (time > 0):
time -= 1
sleep(1)
if (time <= 0):
os.system("shutdown /r /t 0")
if (time == 20):
time = 1200
while (time > 0):
time -= 1
sleep(1)
if (time <= 0):
os.system("shutdown /r /t 0")
#elif (inHours == True):
# if (time == 1):
# Setup #
window = Tk()
window.geometry("385x250")
window.title("PCOFF")
# Codigo #
welcome = Label(window, text="- - PCOFF - -")
welcome.configure(font=("Arial", 15), fg="red")
welcome.pack()
welcome.place(x= 135, y= 5)
instructions = Label(window, text="Seleccione en que formato quieres
programar su apagado/reiniciado")
instructions.pack()
instructions.place(x= 8, y= 30)
program = Entry(window, width=20)
program.pack()
program.place(x= 30, y= 70)
programInSecond = Button(window, text="Segundos",
command=setInSeconds)
programInSecond.pack()
programInSecond.place(x= 30, y= 100)
programInMinutes = Button(window, text="Minutos",
command=setInMinutes)
programInMinutes.pack()
programInMinutes.place(x= 100, y= 100)
programInHours = Button(window, text=" Horas ",
command=setInHours)
programInHours.pack()
programInHours.place(x= 33, y= 130)
programInDays = Button(window, text=" Dias ", command=setInDays)
programInDays.pack()
programInDays.place(x= 100, y= 130)
startCountdown = Button(window, text=" Iniciar ",
command=start)
startCountdown.configure(bg="green")
startCountdown.pack()
startCountdown.place(x= 58, y= 170)
stopCountdown = Button(window, text=" Parar ")
stopCountdown.configure(bg="red")
stopCountdown.pack()
stopCountdown.place(x= 58, y= 200)
# mainloop #
window.mainloop()
global
dentro de la función