【问题标题】:Run two scripts simultaneously in tkinter在 tkinter 中同时运行两个脚本
【发布时间】:2020-06-02 06:57:45
【问题描述】:

我需要在单击一个按钮时同时运行两个脚本。无法使用两个按钮,因为在单击第一个按钮后 gui 冻结并等待第一个程序完成。 代码如下:

import tkinter
import os
import subprocess

window = tkinter.Tk()
window.title("GUI")

def clicked():
     os.system('python inference.py')
     os.system('python extract_frames.py')

    # I used the subprocess approach also but it still waits for the first program to finish

    subprocess.run("python inference.py  & python extract_frames.py",shell=True)


bt = tkinter.Button(window,text="Click Here to start detecting",command=clicked).pack()

window.geometry('400x400')
window.mainloop()

【问题讨论】:

  • 如果您尝试同时运行两个不同的视图,那么您将不得不使用线程。
  • 使用两个subprocess.Popen(...)在不同的进程中运行这两个脚本。

标签: python python-3.x multithreading tkinter subprocess


【解决方案1】:

尝试这样调整:

import tkinter
import os
from subprocess import call
import threading

window = tkinter.Tk()
window.title("GUI")

def clicked():
    #os.system('python inference.py')
    #os.system('python extract_frames.py')

    # I used the threading approach 

    threading.Thread(target=call, args=("python inference.py" ,), ).start()
    threading.Thread(target=call, args=("python extract_frames.py" ,), ).start()



bt = tkinter.Button(window,text="Click Here to start detecting",command=clicked).pack()

window.geometry('400x400')
window.mainloop()

【讨论】:

    【解决方案2】:

    您的代码将如下所示。它只是一个简单的实现。

    from threading import Thread
    import tkinter
    import os
    import subprocess
    
    window = tkinter.Tk()
    window.title("GUI")
    def fun1():
         os.system('python inference.py')
    def fun2():
         os.system('python extract_frames.py')
    
    
    def clicked():
         Thread(target = fun1).start() 
         Thread(target = fun2).start()
    
    
    bt = tkinter.Button(window,text="Click Here to start detecting",command=clicked).pack()
    
    window.geometry('400x400')
    window.mainloop()
    

    【讨论】:

      【解决方案3】:

      您可以使用两个subprocess.Popen(...) 在不同的进程中运行这两个脚本:

      import subprocess
      import tkinter as tk
      
      proclist = []
      
      def clicked():
          proclist.clear()
          for script in ('inference.py', 'extract_frames.py'):
              proc = subprocess.Popen(['python', script])
              proclist.append(proc)
      
      def kill_tasks():
          for proc in proclist:
              if proc and proc.poll() is None:
                  print('Killing process with PID', proc.pid)
                  proc.kill()
          proclist.clear()
      
      root = tk.Tk()
      root.geometry('400x400')
      root.title('GUI')
      
      tk.Button(root, text='Start detecting', width=20, command=clicked).pack()
      tk.Button(root, text='Kill tasks', width=20, command=kill_tasks).pack()
      
      root.mainloop()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-18
        相关资源
        最近更新 更多