在函数期间更新tkinter texvariable标签

时间:2016-04-19 07:38:26

标签: python function tkinter label

我正在尝试制作一个控制发光测试机的程序。 我用tkinter对GUI进行编程,然后我创建了一个按钮来启动一个在机器上进行测试的函数。 该函数在机器上启动3个不同的测试,在GUI中有一个框(ttk.Label(root, textvariable=x)),必须单独更新三个过程的结果。

当我按下按钮时,三个测试正确启动,当功能结束时,文本被正确更新。但我想在函数期间更新它,以显示每个测试的结果,直到函数结束。

我正在寻找并尝试在功能期间无法更新文本,而不是在功能结束时。

有人能帮帮我吗?

这是我使用的函数的代码:

def inittest():
    if fn.initground()=='Y':
        progtext = ('Ground test has started')
        progress.set(progtext)
    elif fn.initground()=='N'
        progtext = ('Ground test has failed')
        progress.set(progtext)
    response = fn.receivedata()
    if len(response) == 1:
        progtext = (progtext + 'Data not received')
        progress.set(progtext)
    else:
        progtext = (progtext + response)
        progress.set(progtext)
    time.sleep(2)

    if fn.initisolate()=='Y':
        progtext = ('Isolate test has started')
        progress.set(progtext)
    elif fn.initisolate()=='N'
        progtext = ('Isolate test has failed')
        progress.set(progtext)
    response = fn.receivedata()
    if len(response) == 1:
        progtext = (progtext + 'Data not received')
        progress.set(progtext)
    else:
        progtext = (progtext + response)
        progress.set(progtext)
    time.sleep(2)

    if fn.initfun()=='Y':
        progtext = ('Function test has started')
        progress.set(progtext)
    elif fn.initfun()=='N'
        progtext = ('Function test has failed')
        progress.set(progtext)
    response = fn.receivedata()
    if len(response) == 1:
        progtext = (progtext + 'Data not received')
        progress.set(progtext)
    else:
        progtext = (progtext + response)
        progress.set(progtext)
    time.sleep(2)

1 个答案:

答案 0 :(得分:0)

好的,经过很长时间的尝试代码,我终于得到了有效的代码。 我在函数启动之前设置了一个全局值,并在每次测试完成后修改它。在此之后,我回想起这个函数,它开始第二次测试,因为我设置的全局值已经改变。 这是我使用的代码。 感谢您的帮助。

global action
action = 1

def inittest():

    global action

    if action == 1:
         def testground():
         global progtext
         if fn.initground()=='Y':
             progtext = ('Ground test has started')
             progress.set(progtext)
         elif fn.initground()=='N'
             progtext = ('Ground test has failed')
             progress.set(progtext)
         response = fn.receivedata()
         if len(response) == 1:
             progtext = (progtext + 'Data not received')
             progress.set(progtext)
         else:
             progtext = (progtext + response)
             progress.set(progtext)
         time.sleep(2)
     action = 2
     testground()
     root.after(1000, inittest)

     if action == 2:

         def testisolate():
         global progtext
         if initisolate()=='Y':
             progtext = (progtext + 'Isolate test has started')
             progress.set(progtext)
         elif fn.initisolate()=='N'
             progtext = (progtext + 'Isolate test has failed')
             progress.set(progtext)
         response = fn.receivedata()
         if len(response) == 1:
             progtext = (progtext + 'Data not received')
             progress.set(progtext)
         else:
             progtext = (progtext + response)
             progress.set(progtext)
         time.sleep(2)
     action = 3
     testisolate()
     root.after(1000, inittest)

     if action == 3:

         def testfun():
         global progtext
         if initfun()=='Y':
             progtext = (progtext + 'Function test has started')
             progress.set(progtext)
         elif fn.initfun()=='N'
             progtext = (progtext + 'Function test has failed')
             progress.set(progtext)
         response = fn.receivedata()
         if len(response) == 1:
             progtext = (progtext + 'Data not received')
             progress.set(progtext)
         else:
             progtext = (progtext + response)
             progress.set(progtext)
         time.sleep(2)
     action = 1
     testfun()