문제

명령 줄에서 결과를 인쇄하는 프로그램을 만들었습니다. (서버이며 명령 줄을 인쇄합니다.)

이제 GUI와 동일한 결과를보고 싶습니다.

명령 줄 결과를 GUI로 리디렉션하려면 어떻게해야합니까?

콘솔 애플리케이션을 간단한 GUI로 쉽게 변환하는 트릭을 제안하십시오.

Linux와 Windows에서 작동해야합니다.

도움이 되었습니까?

해결책

명령 줄 프로그램을 하위 프로세스로 실행하는 스크립트 래퍼를 작성한 다음 텍스트 위젯과 같은 것에 출력을 추가 할 수 있습니다.

from tkinter import *
import subprocess as sub
p = sub.Popen('./script',stdout=sub.PIPE,stderr=sub.PIPE)
output, errors = p.communicate()

root = Tk()
text = Text(root)
text.pack()
text.insert(END, output)
root.mainloop()

스크립트가 귀하의 프로그램입니다. 오류를 다른 색상으로 인쇄 할 수 있습니다.

다른 팁

GUI에 하위 프로세스의 출력을 표시합니다 여전히 실행 중입니다, Python 2와 3에서 작동하는 휴대용 stdlib 전용 솔루션은 배경 스레드를 사용해야합니다.

#!/usr/bin/python
"""
- read output from a subprocess in a background thread
- show the output in the GUI
"""
import sys
from itertools import islice
from subprocess import Popen, PIPE
from textwrap import dedent
from threading import Thread

try:
    import Tkinter as tk
    from Queue import Queue, Empty
except ImportError:
    import tkinter as tk # Python 3
    from queue import Queue, Empty # Python 3

def iter_except(function, exception):
    """Works like builtin 2-argument `iter()`, but stops on `exception`."""
    try:
        while True:
            yield function()
    except exception:
        return

class DisplaySubprocessOutputDemo:
    def __init__(self, root):
        self.root = root

        # start dummy subprocess to generate some output
        self.process = Popen([sys.executable, "-u", "-c", dedent("""
            import itertools, time

            for i in itertools.count():
                print("%d.%d" % divmod(i, 10))
                time.sleep(0.1)
            """)], stdout=PIPE)

        # launch thread to read the subprocess output
        #   (put the subprocess output into the queue in a background thread,
        #    get output from the queue in the GUI thread.
        #    Output chain: process.readline -> queue -> label)
        q = Queue(maxsize=1024)  # limit output buffering (may stall subprocess)
        t = Thread(target=self.reader_thread, args=[q])
        t.daemon = True # close pipe if GUI process exits
        t.start()

        # show subprocess' stdout in GUI
        self.label = tk.Label(root, text="  ", font=(None, 200))
        self.label.pack(ipadx=4, padx=4, ipady=4, pady=4, fill='both')
        self.update(q) # start update loop

    def reader_thread(self, q):
        """Read subprocess output and put it into the queue."""
        try:
            with self.process.stdout as pipe:
                for line in iter(pipe.readline, b''):
                    q.put(line)
        finally:
            q.put(None)

    def update(self, q):
        """Update GUI with items from the queue."""
        for line in iter_except(q.get_nowait, Empty): # display all content
            if line is None:
                self.quit()
                return
            else:
                self.label['text'] = line # update GUI
                break # display no more than one line per 40 milliseconds
        self.root.after(40, self.update, q) # schedule next update

    def quit(self):
        self.process.kill() # exit subprocess if GUI is closed (zombie!)
        self.root.destroy()


root = tk.Tk()
app = DisplaySubprocessOutputDemo(root)
root.protocol("WM_DELETE_WINDOW", app.quit)
# center window
root.eval('tk::PlaceWindow %s center' % root.winfo_pathname(root.winfo_id()))
root.mainloop()

솔루션의 본질은 다음과 같습니다.

  • 하위 프로세스 출력을 백그라운드 스레드에서 큐에 넣습니다.
  • GUI 스레드의 대기열에서 출력을 가져옵니다.

즉, 전화 process.readline() 배경 스레드 -> 큐 -> 기본 스레드에서 GUI 레이블을 업데이트하십시오. 관련된 kill-process.py (폴링 없음 - 사용하는 덜 휴대용 솔루션 event_generate 배경 스레드에서).

STDOUT를 write () 메소드로 리디렉션하는 것은 GUI를 업데이트하는 것이 한 가지 방법 일뿐입니다. 하위 프로세스를 실행하는 것이 더 우아한 솔루션 일 것입니다.

그래도 스더러 만 리디렉션되면 정말로 확신하고 작동한다고 확신합니다!

예제 구식 (GUI 파일 및 테스트 스크립트) :

test_gui.py :

from Tkinter import *
import sys
sys.path.append("/path/to/script/file/directory/")

class App(Frame):
    def run_script(self):
        sys.stdout = self
        ## sys.stderr = self
        try:
            del(sys.modules["test_script"])
        except:
            ## Yeah, it's a real ugly solution...
            pass
        import test_script
        test_script.HelloWorld()
        sys.stdout = sys.__stdout__
        ## sys.stderr = __stderr__

    def build_widgets(self):
        self.text1 = Text(self)
        self.text1.pack(side=TOP)
        self.button = Button(self)
        self.button["text"] = "Trigger script"
        self.button["command"] = self.run_script
        self.button.pack(side=TOP)

    def write(self, txt):
        self.text1.insert(INSERT, txt)

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
        self.build_widgets()

root = Tk()
app = App(master = root)
app.mainloop()

test_script.py :

print "Hello world!"

def HelloWorld():
    print "HelloWorldFromDef!"

내 하찮은 영어 실력에 죄송하다는 말씀을 드리고 싶습니다. 실제로 새로운 자동화 도구에 명령 프롬프트 출력을 인쇄하는 다른 방법을 사용했습니다. 아래 단계를 찾으십시오.

1> 박쥐 파일을 만들고 출력을 로그 파일로 리디렉션합니다. 명령 프롬프트 명령 : tasklist /svc

2> Python 3.x로 해당 파일을 읽으십시오. `ProcessedFile = Open ( 'D : log tasklog.txt', 'r')

3> 피날레 단계.ttk.Label(Tab4, text=[ProcessFile.read()]).place(x=0, y=27)

** 따라서 스크롤 바를 아직이 코드에 포함시키지 않았다는 사실을 알려주세요.

스크린 샷 게시 :

enter image description here

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top