2014-11-03 51 views
0

我有一個用wxPython製作的GUI,當我按下按鈕時,它調用一個從單獨的Python文件導入的函數,並在文本框中顯示該函數的輸出。我想改進它,以便如果函數要求用戶輸入中間執行(如raw_input()),我想要一個新的彈出窗口而不是等待文本框中的raw_input。我一直在瀏覽wxPython文檔,但似乎無法找到類似我想要的東西,所以我想知道這裏有人能否給我任何指示。wxPython彈出調用導入的函數

GUI代碼:

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() 

外部函數的代碼:

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!" 
+0

我們需要看到你的代碼。如果外部函數使用raw_input()提示用戶輸入,則需要重新編寫它,以便它適合具有GUI的應用程序。 – 2014-11-04 00:04:16

+0

這是由其他人編寫的,我被賦予了製作圖形用戶界面的任務,但是就像你說的那樣,這不是我們要走的路。什麼樣的功能或代碼符合我的需求? – olyc 2014-11-04 01:09:43

回答

1

我會通過一個按鈕事件調用外部函數。而不是raw_input,我只是使用帶有「是」或「否」按鈕的wx.MessageDialog。您可以檢查用戶按下哪個按鈕,並相應地繼續。下面是該對話框中的一些鏈接及其他:

如果這一塊,你正在運行的代碼需要很長的時間(即大於第二) ,那麼它可能會阻止wx的主循環並導致應用程序無響應。如果是這樣的話,那麼你需要將這些代碼移動到一個線程中。下面的文章將幫助您與行動方針: