Tkinter,在执行的时候,试着继续使用这个程序

2024-05-03 10:36:39 发布

您现在位置:Python中文网/ 问答频道 /正文

在执行以下循环时,如何保持程序的执行?你知道吗

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()

Tags: 程序getdatetimetimevarcallbacknowha
1条回答
网友
1楼 · 发布于 2024-05-03 10:36:39

您可以使用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()

相关问题 更多 >