【发布时间】:2015-03-17 07:59:41
【问题描述】:
我的 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
提前致谢。
【问题讨论】:
标签: python tkinter subprocess