如果在执行以下循环时如何保持程序的执行?
def callback():
var=OpenHour.get()
var1=CloseHour.get()
actual = 0
if not validateDate(var, var1):
tkMessageBox.showinfo("Error","Datos o formato incorrecto, deberia ser hh:mm")
else:
while var != actual:
actual = datetime.datetime.now().time().strftime('%H:%M')
print "ya acabe voy al second while"
while var1 != actual:
actual = datetime.datetime.now().time().strftime('%H:%M')
tkMessageBox.showinfo("Exito","Se ha terminado el ciclo")
#tkMessageBox.showinfo("Exito","La programacion se ha \nrealizado de la manera correcta")
b = Button(VentanaPersiana, text="Programar", width=10, command=callback)
b.pack()
答案 0 :(得分:0)
您可以使用root.after(miliseconds, function_name, argument)
执行延迟功能,root.mainloop()
(和程序)将正常工作。
您可以使用datetime
将文本转换为datetime对象,减去两个日期并获得以毫秒为单位的差异。
try:
import tkinter as tk
import tkinter.messagebox as tkMessageBox
print("Python 3")
except:
import Tkinter as tk
import tkMessageBox
print("Python 2")
from datetime import datetime as dt
# --- functions ---
def callback():
var_open = open_hour.get()
var_close = close_hour.get()
if not validateDate(var_open, var_close):
tkMessageBox.showinfo("Error", "Incorrect times")
else:
# current date and time
current = dt.now()
print('current:', current)
# convert strings to `time` object
time_open = dt.strptime(var_open, '%H:%M').time()
time_close = dt.strptime(var_close, '%H:%M').time()
print('time_open:', time_open)
print('time_close:', time_close)
# combine `current date` with `time`
today_open = dt.combine(current, time_open)
today_close = dt.combine(current, time_close)
print('today_open:', today_open)
print('today_close:', today_close)
# substract dates and get milliseconds
milliseconds_open = (today_open - current).seconds * 1000
milliseconds_close = (today_close - current).seconds * 1000
print('milliseconds_open:', milliseconds_open)
print('milliseconds_close:', milliseconds_close)
# run functions with delay
root.after(milliseconds_open, show_open_message, var_open)
root.after(milliseconds_close, show_close_message, var_close)
def validateDate(time1, time2):
# TODO: check dates
return True
def show_open_message(arg):
tkMessageBox.showinfo("Info", "Open time: " + arg)
def show_close_message(arg):
tkMessageBox.showinfo("Info", "Close time: " + arg)
# --- main ---
root = tk.Tk()
open_hour = tk.StringVar()
close_hour = tk.StringVar()
e1 = tk.Entry(root, textvariable=open_hour)
e1.pack()
e2 = tk.Entry(root, textvariable=close_hour)
e2.pack()
b = tk.Button(root, text="Start", command=callback)
b.pack()
root.mainloop()