python - 使用 tkinter 运行另一个 python 脚本
问题描述
我已经使用 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='#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
, eg中的函数内部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()
推荐阅读
- javascript - 如何在本机反应中使用 Jest 测试 lodash 的去抖动功能?
- html - 当我将鼠标悬停在它上面时,我无法弄清楚如何阻止我的导航栏突出显示我的活动链接
- keras - 输出层正则化实现
- java - 使用会话加载图像名称
- getstream-io - 仅将频道过滤到用户正在审核的频道
- c# - .net core 2.2 windows服务完全启动后如何配置托管服务?
- ruby-on-rails - 在使用 minitest 在 ruby 中测试模型时,我在 def setup 中保存数据,我得到了这条线的含义
- plotly - 绘制 groupby 变换
- math - 计算机如何计算负(-)和正(+)无穷大?
- java - 如何在java中从double转换为大于long的值