【发布时间】:2014-11-03 23:59:33
【问题描述】:
我有一个使用 wxPython 制作的 GUI,当我按下按钮时,它会调用我从单独的 Python 文件中导入的函数,并在文本框中显示该函数的输出。我想改进它,以便如果函数在执行过程中要求用户输入(如 raw_input()),我希望出现一个新的弹出窗口,而不是 raw_input 在文本框中等待。我一直在浏览 wxPython 文档,但似乎找不到任何与我想要的相似的东西,所以我想知道这里是否有人可以给我任何指示。
界面代码:
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!"
【问题讨论】:
-
我们需要查看您的代码。如果外部函数使用 raw_input() 提示用户输入,则需要重写它,使其适合带有 GUI 的应用程序。
-
这是由其他人编写的,我被赋予了制作 GUI 的任务,但就像你说的那样,这不是要走的路。什么样的函数或代码能满足我的需求?
标签: python user-interface wxpython