wxPython popup from calling imported function

2019-09-02 19:36发布

问题:

I have a GUI made with wxPython that calls a function that I imported from a separate Python file when I press a button, and shows the output of that function in a text box. I want to improve it so that if the function asks for user input mid-execution (like a raw_input()), I want a new popup window to appear instead of the raw_input waiting in the text box. I've been looking through the wxPython documentation but can't seem to find anything that resembles what I want, so I was wondering if anyone here could give me any pointers.

GUI code:

import sys
import os
import re
import subprocess
import threading
import wx
import errno, os, stat, shutil
import extern_func

#this object redirects the external function output to the text box
class RedirectText(object):
    def __init__(self,aWxTextCtrl):
        self.out=aWxTextCtrl

    def write(self,string):
        self.out.WriteText(string)

#GUI code here
class progFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, title="functionGUI", size=(800, 600), style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER)
        panel = wx.Panel(self)

        #more things....


        self.runButton = wx.Button(panel, wx.ID_OK, "Run", pos=(200, 300))

        self.out=wx.TextCtrl(panel, style=wx.TE_MULTILINE|wx.VSCROLL|wx.TE_READONLY, pos = (300, 50), size=(500, 200))

        #Run button event
        self.Bind(wx.EVT_BUTTON, self.OnRun, self.runButton)

        #command prompt output to frame
        redir=RedirectText(self.out)
        sys.stdout=redir
        self.Show()

    def OnRun(self, event):
        t=threading.Thread(target=self.__run)
        t.start()

    #external function call
    def __run(self):
        externFunc()

if __name__ == '__main__':
app = wx.App(False)
progFrame(None)
app.MainLoop()

External function code:

import sys

def externFunc():
    print "Starting execution..."
    #a bunch of code...
    cont = raw_input("Something has gone wrong. Do you still want to continue?")
    if(cont.lower() == "n")
        sys.exit(0)
    #more function code...
    print "Success!"

回答1:

I would call the external function via a button event. Instead of raw_input, I would just use a wx.MessageDialog with a yes or no button on it. You can check which button the user pressed and continue or not accordingly. Here are some links on that dialog and others:

  • http://wxpython.org/Phoenix/docs/html/MessageDialog.html
  • http://www.blog.pythonlibrary.org/2010/06/26/the-dialogs-of-wxpython-part-1-of-2/
  • http://zetcode.com/wxpython/dialogs/

If this piece of code you are running takes a long time (i.e. greater than a second), then it is probably going to block wx's mainloop and cause the application to become unresponsive. If that is the case, then you'll need to move this code into a thread. The following articles will help you with that course of action:

  • http://wiki.wxpython.org/LongRunningTasks
  • http://www.blog.pythonlibrary.org/2010/05/22/wxpython-and-threads/