Python Gui冻结子进程

时间:2016-09-20 05:31:13

标签: python python-3.x tkinter

我正在为ffmpeg进程编写一个小GUI,但是当我运行子进程时,GUI会冻结。我已经从具有运行子流程的流程的类中拆分了GUI类。

有人能指出我正确的方向吗?我对有问题的坏代码发表评论。

在课程的下方。

更新

我有线程运行,但无法使用和事件停止线程,我不确定我是否正确使用它。

我已将代码更新为MySub class

中的代码
#!/usr/bin/env python
import threading
import subprocess
import re
import time
import atexit


class MySub:
    def __init__(self):
        self.t = threading.Thread(target=self.getlog, daemon=True)
        # return self

    def getlog(self):
        # Here is my sub process

        p = subprocess.Popen(['ffmpeg', '-y', '-i', '/home/brett/Documents/slayer/univ.webm', '-i',
                              '/home/brett/Documents/slayer/unis.webm',
                              '-q:v', '0', 'slayer_2.mp4'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
                             universal_newlines=True)
        while p.poll() is None:
            # time.sleep(.5)
            p.poll()
            for line in p.stdout:
                prog = re.compile("[time]=\d+:\d+:\d+\.\d+")
                current_time = prog.search(line)
                if current_time:
                    print(current_time.string[current_time.start(0) + 2:current_time.end(0)])
            self.getlog()

    def close_it(self):
        eh = threading.Event()
        time.sleep(.5)
        eh.set()
        self.t.join()

    def new_thread(self):
        self.t.start()

这是gui班

#!/usr/bin/env python
from tkinter import *
from ttk import *
from tkinter.filedialog import askopenfilename
from tkinter.messagebox import showerror
from mysub import MySub

# import contextlib
# import redirect_stdout


class FfGui(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.t1 = StringVar()
        self.t2 = StringVar()
        self.parent = parent
        self.initUI()

    def initUI(self):
        my_sub = MySub
        self.parent.title("FFGui")
        self.parent.iconbitmap('@myico.xbm')

        self.pack(fill=BOTH, expand=True)

        frame0 = Frame(self)
        frame0.pack(fill=BOTH)

        frame1 = Frame(self)
        frame1.pack(fill=BOTH, pady=5, padx=5)

        frame2 = Frame(self)
        frame2.pack(fill=BOTH, padx=5)

        frame3 = Frame(self)
        frame3.pack(fill=BOTH, pady=5, padx=5, expand=True)

        frame4 = Frame(self)
        frame4.pack(fill=X, pady=5)

        lbl = Label(frame0, text="Mux Movie")
        lbl.pack(fill=X, side=RIGHT, expand=True)

        aub = Entry(frame1, textvariable=self.t1)
        aub.pack(fill=BOTH, side=LEFT, expand=True)
        aub1 = Entry(frame2, textvariable=self.t2)
        aub1.pack(fill=BOTH, side=LEFT, expand=True)

        aubb = Button(frame1, text="VIDEO", command=self.load_video)
        aubb.pack(side=LEFT, padx=2)
        aubb1 = Button(frame2, text="AUDIO", command=self.load_audio)
        aubb1.pack(side=LEFT, padx=2)

        my_progress = Progressbar(frame3, mode="indeterminate")
        my_progress.pack(fill=BOTH)

        doit = Button(frame4, text="CANCEL", width=9, command=exit)
        doit.pack(side=RIGHT, padx=5)
        closeit = Button(frame4, text="RUN", width=9, command=my_sub.getlog)
        closeit.pack(side=RIGHT)

    def load_video(self):
        video = self.load_file()
        self.t1.set(video)

    def load_audio(self):
        audio = self.load_file()
        self.t2.set(audio)

    def load_file(self):
        fname = askopenfilename()
        if fname:
            try:
                return fname
            except:  # <- naked except is a bad idea
                showerror("Open Source File", "Failed to read file\n'%s'" % fname)
            return

    # def getlog(self):
    #     process = subprocess.Popen(['ffmpeg', '-y', '-i', '/home/brett/Documents/slayer/univ.webm',
    #                                 '-i', '/home/brett/Documents/slayer/unis.webm',
    #                                 '-q:v', '0', 'slayer_2.mp4'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
    #                                universal_newlines=True)
    #     # print(process.stdout.read())
    #     for line in process.stdout:
    #         prog = re.compile("[time]=\d+:\d+:\d+\.\d+")
    #         current_time = prog.search(line)
    #         if current_time:
    #             print(current_time.string[current_time.start(0) + 2:current_time.end(0)])


def main():
    root = Tk()
    root.geometry("350x150+300+300")
    app = FfGui(root)
    root.mainloop()


if __name__ == '__main__':
    main()

0 个答案:

没有答案