使用tkinter运行另一个python脚本
我已经使用tkinter创建了一个GUI,我希望在打开GUI时运行另一个python脚本。这是我到目前为止所做工作的示例代码使用tkinter运行另一个python脚本,python,tkinter,Python,Tkinter,我已经使用tkinter创建了一个GUI,我希望在打开GUI时运行另一个python脚本。这是我到目前为止所做工作的示例代码 window = Tk() window.configure(bg='#101d25') window.maxsize(width=580, height=450) window.minsize(width=580, height=450) title = Label(window, text='Face clustering', bg='#232d36', fg='#
window = Tk()
window.configure(bg='#101d25')
window.maxsize(width=580, height=450)
window.minsize(width=580, height=450)
title = Label(window, text='Face clustering', bg='#232d36', fg='#979ca0', font=('Ink Free', 30,
'bold'))
title.pack(side=TOP, fill=X)
process_label = Label(window, text='Processing files', fg='#979ca0', bg='#101d25', font=('Ink Free',
14, 'bold'))
process_label.place(x=70, y=150)
style = ttk.Style()
style.theme_use('clam')
TROUGH_COLOR = '#101d25'
BAR_COLOR = '#979ca0'
style.configure("red.Horizontal.TProgressbar", troughcolor=TROUGH_COLOR, bordercolor=TROUGH_COLOR,
background=BAR_COLOR, lightcolor=BAR_COLOR, darkcolor=BAR_COLOR)
progress_bar = ttk.Progressbar(window, style="red.Horizontal.TProgressbar", orient=HORIZONTAL,
length=300,mode="determinate")
progress_bar.place(x=50, y=200)
progress_bar.start()
os.system('python sample.py')
progress_bar.stop()
window.mainloop()
我希望进度条一直运行,直到sample.py完成执行。正在执行该文件,但不显示GUI。希望找到解决办法
提前感谢您需要在另一个线程中运行脚本:
import threading
...
progress_bar.place(x=50, y=200)
def run_script():
progress_bar.start()
os.system('python sample.py')
progress_bar.stop()
threading.Thread(target=run_script, daemon=True).start()
window.mainloop()
如果主代码块位于sample.py
中的函数内,例如main()
,则最好导入该函数并直接调用该函数:
import threading
from sample import main
...
def run_it():
progress_bar.start()
main()
progress_bar.stop()
threading.Thread(target=run_it, daemon=True).start()
我不知道os.system是否可以在脚本完成时通知您,但也许您可以尝试使用
after
方法。
我将附上我不久前编写的GUI中的一个示例
def checkForComplete(self):
print(self.createStatus)
if self.createStatus == "notStarted":
self.createStatus = "started"
self.progressbar.configure(style = "green.Horizontal.TProgressbar")
self.progressbar.start()
self.after(100, self.checkForComplete)
elif self.createStatus == "succes":
self.progressbar.stop()
self.createStatus = "notStarted"
self.progressbar['value'] = 200
self.progressbar.update_idletasks()
elif self.createStatus == "fail":
self.progressbar.stop()
self.createStatus = "notStarted"
self.progressbar.configure(style = "red.Horizontal.TProgressbar")
self.progressbar['value'] = 200
self.progressbar.update_idletasks()
else: #its started, that means we are waiting for the action to complete
self.after(100, self.checkForComplete)
不是答案,但是-Python不是shell脚本-您可以使用它来构造代码…如果您希望避免线程,此解决方案将对您帮助最大。感谢您提供的代码。我只是让进度条在不确定模式下运行,并在os.system下面添加了progressBar.stop()行。我会把这张纸条留到以后。一切正常,工作正常,谢谢你的建议