Python tkinter,在子进程开始之前做一些事情
Python tkinter, do something before subprocess starts
我的 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
提前致谢。
Tkinter 仅在无事可做时才更新图形。如果您在真正要更新的小部件上使用更新方法,则可以强制刷新 GUI。
tk.updates()
我的 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
提前致谢。
Tkinter 仅在无事可做时才更新图形。如果您在真正要更新的小部件上使用更新方法,则可以强制刷新 GUI。
tk.updates()