python-3.x - Windows 程序没有响应 tkinter gui 的问题
问题描述
我创建的程序有问题,我正在尝试制作一个程序,该程序可以对使用 dism 的驱动器进行映像。当 dism 命令在 cmd 中运行时,它给出了一个完成百分比,所以我试图取这个百分比(使用正则表达式)并将它放在我自己的进度条 gui 中但是当我运行我的程序时,windows 给了我一个程序不是响应错误,我不知道为什么。程序继续在后台运行,完成后错误消失,但进度条不更新。我有一种感觉,这是因为我的 run 函数有自己的循环,所以 tkinter gui 可能在该循环完成之前不会更新。如果有人可以证实这一点或给我其他推理,将不胜感激。另外,如果您对解决此问题有任何想法,我会全力以赴。
import shlex
from tkinter import *
from tkinter import ttk
def run(command):
progressBar['maximum'] = 100
process = subprocess.Popen(shlex.split(command), stdin= subprocess.PIPE
,stdout=subprocess.PIPE)
while True:
output = process.stdout.readline().decode()
if output == '' and process.poll() is not None:
break
if output:
matchObj = re.search(r'\d+\.\d%',output.strip())
if matchObj:
percentNum = re.search(r'\d+\.\d',matchObj.group())
progressBar["value"] =
round(float(percentNum.__getitem__(0)))
print(type(progressBar["value"]))
print(progressBar["value"])
else:
print(output.strip())
rc = process.poll()
progressBar["value"] = 0
return rc
root = Tk()
root.title('Progress Bar')
root.geometry("300x100")
buttonFrame = LabelFrame(text="Options")
buttonFrame.grid(column=0,row=0)
backupCmd = 'Dism /Capture-Image /ImageFile:F:\my-windows-partition.wim
/CaptureDir:"E:" /name:Windows \n'
button1 = Button(master=buttonFrame, text="Backup",command= lambda:
run(backupCmd))
button1.grid(column = 0, row = 0)
restoreCmd = ''
button2 =Button(master=buttonFrame, text="Restore",command= lambda:
run(restoreCmd))
button2.grid(column = 50, row = 0)
button3 =Button(master=buttonFrame, text="Exit",command= lambda: exit())
button3.grid(column = 100, row = 0)
progressBar = ttk.Progressbar(root, orient="horizontal",
length=286,mode="determinate")
progressBar.grid(column = 0, row = 3, pady=10)
root.mainloop()
解决方案
我弄清楚了这个问题。对于任何为此苦苦挣扎的人,您需要添加调用以更新进度条。对我来说,我说,progressBar.update 在我的运行方法中
推荐阅读
- javascript - 将角度服务返回类字段作为可观察的
- flutter - 相当于 Dart 中 Swift 的 if let 和 guard let
- c# - Cosmos DB 序列化问题引发错误,将 Int 读取为 Double
- css - 如何在 CSS 中并排设置背景图像
- powershell - 如何让脚本通过 Powershell 为所选目录中的所有子文件夹运行?
- java - 反射性地基于静态类生成可实例化的类
- html - Outlook 中的文本添加了额外的间距/填充或行高问题
- powershell - 如何同时激活 Anaconda 环境和 Typescript 观察者?
- java - 如何在 CMD 中分离 Java 程序的输出?
- python - 如何在广度优先树中存储节点到根的距离?