【问题标题】:How to display a value on Tkinter window from a function every second?如何每秒从一个函数在 Tkinter 窗口上显示一个值?
【发布时间】:2022-01-13 18:52:10
【问题描述】:

所以我正在研究一个函数,每秒都会做一些事情(转动电机)。为了计算秒数,我使用 time.sleep(1),因为我不知道任何其他等待方式。

def wash_loop(wash_time):
    
    count = 0
    dist = 0
    global error_flag
    error_flag = 0
    
    while (count < wash_time):
        if(count%2==0):#going forward
            GPIO.output(Motor_IN1,GPIO.HIGH)
            GPIO.output(Motor_IN2,GPIO.LOW)
            GPIO.output(Motor_EN,GPIO.HIGH)
            print(count)
            time.sleep(MOTOR_SLEEP)
        
        else:#going backwards
            GPIO.output(Motor_IN1,GPIO.LOW)
            GPIO.output(Motor_IN2,GPIO.HIGH)
            GPIO.output(Motor_EN,GPIO.HIGH)
            print(wash_time - count) #want to display this on a Tkinter window instead
            time.sleep(MOTOR_SLEEP)

        count+=1
        dist = distance_check()
        if(dist < CRITICAL_DIS):
           error_alert()
           break
        
        if(count>=wash_time):
            GPIO.output(Motor_EN, GPIO.LOW)
            break

我试图在其中执行此操作的 Tkinter 函数如下所示:

def wash_window():
    
    #create the main window for GUI control applcation
        window2 = TK.Tk()
        temp = TK.IntVar()
        
        window2.geometry(WINDOW_GEOMETRY)
        window2.title("Wash Cycle ")
        
        #create the frame that will hold instructions
        frame0 = TK.Frame(window2)
        frame0.pack(side=TK.TOP)
        
        TK.Label(frame0, text="You've selected custom wash cycle").grid(row=0,column=0)
        TK.Label(frame0, text="Please select the water temperature for the wash.")\
                .grid(row=1,column=0)
        
        frame1 = TK.Frame(window2)
        frame1.pack(side=TK.TOP)
        
        temp.get()
        hot_button = TK.Radiobutton(frame1, text="HOT", variable=temp, value=1 ).grid(row=0, column=0)
        cold_button = TK.Radiobutton(frame1, text="COLD", variable=temp, value=2).grid(row=0,column=1)
        warm_button = TK.Radiobutton(frame1, text="WARM", variable=temp, value=3).grid(row=0,column=2)
        
        #create the frame that will hold control entry in the window
        frame2 = TK.Frame(window2)
        frame2.pack(side=TK.TOP)
        TK.Label(frame2, text = "Please enter the time below for the wash cycle").grid(row=0,column=0)
        user_entry = TK.Entry(frame2)
        user_entry.grid(row=1,column=0)
        
        frame3= TK.Frame(window2)
        frame3.pack(side=TK.TOP)
        
           
        start_button = TK.Button(frame3, text="START", command = lambda: wash_cycle(user_entry,temp)).grid(row=0, column=0)
       # stop_button = TK.Button(frame3, text="STOP", command = lambda: wash_cycle(user_entry,temp)).grid(row=0, column=1)
        quit_button = TK.Button(frame3, text="QUIT", command = window2.destroy).grid(row=0, column=2)

我正在尝试做的是在这个 Tkinter 窗口上显示倒计时(从输入的时间到 0),只要人们在窗口中按下开始。这与使用 after() 函数不同,因为我想显示一个只执行一次的函数的值,只是它有一个循环。 谢谢!

【问题讨论】:

  • 我们经常在这里收到有关基于计时器执行代码的问题。请对after 方法进行一些研究。
  • 您可以在线程中运行wash_loop() 并更新链接到函数内的tkinter Label 小部件的tkinter StringVar

标签: python tkinter time python-multithreading


【解决方案1】:

所以你可以做的是使用线程。

你需要:

从线程导入计时器

然后你创建一个计时器

your_timer = Timer(the_seconds_you_want_to_wait, the_function_to_call)

您正在使用 this 调用一个函数,并且在该函数中您可以在 tkinter 窗口中显示任何内容。 我建议您在这里做的是创建一个标签,然后更改它的属性。 例如,如果你想在标签上显示一个数字:

L1 = Label(root, text="your text here")
L1.pack()

现在,如果您想稍后编辑:

L1.configure(text="your new text here")

your_timer.start()

在函数结束时,您需要启动计时器以创建一个循环或仅为它创建一个循环。

【讨论】:

  • 我如何在我的 Tkinter 窗口中显示它?
  • 如果你想在屏幕上显示一个标签,你只需要将小部件添加到标签上:l1 = Label(window2, ...)
猜你喜欢
  • 1970-01-01
  • 2016-12-25
  • 2018-05-11
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 2016-09-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多