Python tkinter,在子进程启动之前做一些事情

时间:2015-03-17 07:59:42

标签: python tkinter subprocess

我的Python tkinter程序确实存在问题。基本上我想做的就是按一个按钮启动子进程,并通过更改标签的值来指示子进程正在运行。子进程需要一些时间,问题是标签总是等待子进程完成更改,我不明白,因为我使用变量首先更改标签,然后继续子进程。这是代码:

def program_final():
    start = False
    while True:
        if start == False: 
            v.set("scanning...")
            label.pack()
            start = True
        else:
            # p = subprocess.Popen('sudo nfc-poll', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)     # opens a subporocess which starts nfc-polling in background
            p = subprocess.Popen('ping 8.8.8.8', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
            counter = 0
            output = ""
            lines = []
            while True:
                line = p.stdout.readline() # this while loop iterates through the output lines of the 
                lines.insert(counter, line) # subproccess and saves the whole result into a list
                counter = counter +1 # the result will be needed to set output
                if counter == 9:
                    break

            if lines[6][7:10] == 'UID': # check if UID line is present
                output = output + "Tag found!\n" + lines[6][7:] # if yes the output string gets added the UID of the tag
            elif lines[6][7:10] != 'UID': # if the UID line is not present which means no tag is found
                output = output + "No tag found!\n" # the output is set to no tag found

            text.delete(1.0, END) # old tag infos are getting deleted out of texfield
            text.insert(INSERT, output) # tag infos or 'no tag found' message is added to the textfield
            break

提前致谢。

1 个答案:

答案 0 :(得分:0)

只有在没有其他事情要做时才更新图形。如果在要更新的小部件上使用更新方法,则可以强制GUI刷新。

tk.updates()