开发者

wxPython - capturing shell output in a textctrl widget?

开发者 https://www.devze.com 2023-03-18 08:33 出处:网络
I\'m learning python and wxPython at the same time... :)So, I have a set of python files that run fine from the command line: ./scan -d test ~/Pictures -- for instance will create a database of my pic

I'm learning python and wxPython at the same time... :) So, I have a set of python files that run fine from the command line: ./scan -d test ~/Pictures -- for instance will create a database of my pictures called "test."

I've been working diligently on a front end for this (I didn't write the original command line python files), and I can get it to run using:

def bt_ScanUpdateClick(self, event):
    self.SetSizeWH(450,360)

    ## DEBUG
    self.tc_MainDatabase.Value = "test.db"


    if self.tc_MainDatabase.Value == "":
        self.LogWindow.Va开发者_JAVA百科lue += "ERROR:\tNo database name selected!\n"
    else:
        scanCMD = "./scan -d " + self.tc_MainDatabase.Value + " "

        numLines=0
        maxLines=(int(self.multiText.GetNumberOfLines()))

        if self.multiText.GetLineText(numLines) == "":
            self.LogWindow.Value += "ERROR\tNo folder selected to scan!\n"
        else:
            self.LogWindow.Value += "Running Scan...\n\n"
            while (numLines < maxLines):
                scanCMD += str(self.multiText.GetLineText(numLines)) + " "
                numLines += 1

            self.LogWindow.Value += scanCMD

            p = subprocess.Popen([scanCMD],shell=True,stdout=subprocess.PIPE,stdin=subprocess.PIPE)
            self.LogWindow.Value += p.communicate()[0]

This presents a problem for me:

  1. Once I click the Button to activate this, it doesn't actually even get to the self.SetSizeWH(450,360) part of the function. It just stays in a "down" state. When the command is done, the app comes back to life with everything in the output window (self.LogView) already... I would like to get the output in a more 'realtime' way... possible?

Any thoughts?


Roughly speaking, screen updates happen during each iteration of the event loop. While your code is running, the event loop is stuck in the same iteration and thus screen updates can't happen.

To work around this you need to run your long-running task in a separate thread or process. Because wxPython is single-threaded, any updates to the GUI (such as writing the lines of output) must be done in the main thread. To do that, your worker thread can use wx.CallAfter which will schedule a command to be run in the GUI thread at its earliest convenience.

For more information see the page Long Running Tasks on the wxPython wiki, and see this answer to the question wxPython: Problems with thread. on this website.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号