【问题标题】:Python "root.after(1000, my_test)". It cycle every second but twicePython“root.after(1000, my_test)”。它每秒循环一次,但两次
【发布时间】:2018-04-26 01:27:03
【问题描述】:

我的脚本有问题,希望您能帮助我找到根本原因。

在我下面的测试脚本中,我使用函数 "root.after(1000, update_timeText)" 理论上每 1000 毫秒执行一次计数器 "update_timeText" (每一秒)。

此功能还每秒更新一次 GUI,显示 1、2、3、4,...。我第一次执行工作完美,每秒执行一次“update_timeText”,只有一次 GUI 每秒正确更新一次1、2、3、4、……

但是,如果我停止时钟并重新启动计数器,GUI 会每秒更新一次,但每秒会显示 1、3、5、7 ...... “有趣”的情况是,在现实中循环运行了两次。如果我打印变量timer [0],这个变量显示1、2、3、4....但是由于函数“root.after(1000, update_timeText)” em> 每显示 1、3、5、7 时更新一次 GUI。

现在,如果我再次停止时钟并重新启动,现在 GUI 将显示 1, 4, 7, 11, 14.... 但现实中的变量 timer [0] 是数着 1,2,3,4,5... 但真的很快。

下面是我的代码;希望有人能帮我解决这个问题

感谢您的光临。

    def update_timeText():
        global temp
        global degree_sign
        global temp_b
        global temp_c
        if (state):
            global timer
            timer [0] +=1
            if (timer[0] >= 60): ## timer [0] Seconds     
                timer[1] += 1
                timer[0]  = 0
            if (timer[1] >= 60):  ## timer [1] Minutes
                timer[2] += 1
                timer[1]  = 0
            if (timer[2] == 24):  ## timer [2] Hours    
                timer[2]  = 0
            timeString = pattern.format(timer[2], timer[1], timer[0])
            timeText.configure(text=timeString)
        root.after(1000, update_timeText)
    def update_temp():
        global timer
        global loga
        global temp
        global degree_sign
        global temp_b
        global temp_c
        global temp_alter
        if (timer[0] == 1):
            temp = a.read_holding_registers(17415, 1)
            temp_alter = a.read_holding_registers(1031, 1)
            temp_a = str(temp)
            temp_b = temp_a.replace("[", "").replace("]", "")
            temp_c = temp_b[:2]+'.'+temp_b[2:]
            if (temp_alter >= [40]):
                degree_sign= u'\N{DEGREE SIGN}'
                Label(text=(str(temp_c)) + degree_sign + "C",fg = "red", font=("Arial", 12)).place(x=210,y=443)
            if (temp_alter <= [39]):
                degree_sign= u'\N{DEGREE SIGN}'
                Label(text=(str(temp_c)) + degree_sign + "C",fg = "green", font=("Arial", 12)).place(x=210,y=443)
        if (timer[0] == 10 or timer[0] == 20 or timer[0] == 30 or timer[0] == 40 or timer[0] == 50 or timer[0] == 60):
            loga.write("Time Stamp : Hours " +(str(timer[2]))+" ; Minutes "+(str(timer[1]))+" ; Seconds "+(str(timer[0]))+"\n")
            loga.write("Temp Stamp : "+(temp_c) + degree_sign + "C"                             "\n")
        if (timer[1] == 1):
            end_time1 = datetime.now()
            endd1 =(end_time1.strftime("%A %d %b %y, %H:%M"))
            def pause():
                global state
                state = False
                global timer
                timer = [0, 0, 0, 0]
            pause()
            process = "PASSED" 
            Label(text="PASSED      ", fg = "green", font=("Arial", 12)).place(x=210,y=543)
            loga.write("==============================================\n")
            loga.write("My Company.                                   \n")
            loga.write("Assy PN   #: 00000-00 Rev. A                  \n")
            loga.write("Serial    #: N/A                              \n")
            loga.write("Start Date: "+str(startt)+"                   \n")
            loga.write("End Date  : "+str(endd1)+"                    \n")
            loga.write("Station   : TEST                              \n")
            loga.write("Status    : "+str(process)+"                  \n")
            loga.write("==============================================\n")
            loga.close() ##to close logfile
            write = a.write_single_register(8245, 3)
            time.sleep(0.050)
            write = a.write_single_register(7668, 3)
            time.sleep(0.050)
            connect_btna.config(state = NORMAL)
            start_btna.config(state = DISABLED)
            abort_btna.config(state = DISABLED)
        root.after(1000, update_temp)
    def start():
        global state
        state = True  
        ##Starting Set Profile
        write = a.write_single_register(8245, 1)
        ##Lock Door Enabled
        Label(text = "DOOR  LOCKED...     ",fg = "green", font=("Arial", 12)).place(x=210,y=343)
    start()
if status == "":
    messagebox.showinfo('Process App 1.00', "An Error has occurred, Please, restart the app. Click OK to continue")
    write = a.write_single_register(8245, 3)
    time.sleep(0.050)
    write = a.write_single_register(7668, 3)
    time.sleep(0.050)
    loga.write("An Error has accurred.....\n")
    write = a.close()
    sys.exit()
    root.destroy()
    quit()
if status == "Fail":
    messagebox.showinfo('Process App 0.00', "An Error has occurred, Please, restart the app. Click OK to continue")
    root.destroy()
    quit() 
def pause():
    global state
    state = False
def reset():
    global timer
    timer = [0, 0, 0]
    timeText.configure(text='00:00:00')
def exist():
    root.destroy()
timer = [0, 0, 0]
pattern = '{0:02d}'+" h "+' {1:02d}'+" m "+' {2:02d}'+" s "
timeText = tk.Label(text="00:00:00", font=("Tahoma", 12))
timeText.pack()
timeText.place_configure(x=210,y=393)
update_temp()
update_timeText()
root.mainloop()

【问题讨论】:

  • 你显然不止一次地调用了update_timeText(),在程序的其他地方——每一个都会导致它自己的每秒一次的重复序列。 “不要那样做”真的是不用看相关代码就可以说的全部。
  • 谢谢@jasonharper 我在 def update_timeText() 之外有函数 update_timeText() ,仅此而已。如果你愿意,我可以分享我的代码
  • 请考虑添加代码示例,或修改您在此问题中发布的示例。就目前而言,它的格式和范围使我们很难为您提供帮助;这是一个great resource,可以帮助您开始。祝你的代码好运!
  • 嗨@ReblochonMasque 我添加了更多代码,如果有帮助请告诉我。感谢您的帮助。
  • 嗨@jasonharper 只是为了让你知道我找到了问题的根源。函数 "root.after(1000, update_timeText)" 它不在循环中,所以,我在 "timeText.configure(text=timeString)" 下移动,这解决了我的问题。感谢您的帮助

标签: python-3.x tkinter


【解决方案1】:

在分析我的脚本之后,我发现它没有正确嵌套的属于函数,这就是我所拥有的

        timeString = pattern.format(timer[2], timer[1], timer[0])
        timeText.configure(text=timeString)
    root.after(1000, update_timeText)

在我正确嵌套了 root.after(1000, update_timeText) 之后,我的测试脚本作为冠军运行并运行。

        timeString = pattern.format(timer[2], timer[1], timer[0])
        timeText.configure(text=timeString)
        root.after(1000, update_timeText)

谢谢大家, F卡西恩

【讨论】:

    猜你喜欢
    • 2022-01-13
    • 2016-06-19
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-13
    • 1970-01-01
    相关资源
    最近更新 更多