调用导入函数时的 wxPython 弹出窗口 [英] wxPython popup from calling imported function
问题描述
我有一个用 wxPython 制作的 GUI,当我按下按钮时,它调用我从单独的 Python 文件导入的函数,并在文本框中显示该函数的输出.我想改进它,以便如果函数在执行过程中要求用户输入(如 raw_input()),我希望出现一个新的弹出窗口,而不是在文本框中等待的 raw_input.我一直在浏览 wxPython 文档,但似乎找不到与我想要的内容相似的任何内容,所以我想知道这里是否有人可以给我任何指示.
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.
界面代码:
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
,我将只使用带有是或否按钮的 wx.MessageDialog
.您可以检查用户按下了哪个按钮并相应地继续.以下是该对话框和其他对话框的一些链接:
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/
如果您运行的这段代码需要很长时间(即大于一秒),那么它可能会阻塞 wx 的主循环并导致应用程序无响应.如果是这种情况,那么您需要将此代码移动到一个线程中.以下文章将帮助您完成该操作:
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/
这篇关于调用导入函数时的 wxPython 弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!