【问题标题】:wxPython popup from calling imported function调用导入函数的 wxPython 弹出窗口
【发布时间】: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


【解决方案1】:

我会通过按钮事件调用外部函数。而不是raw_input,我只会使用wx.MessageDialog,上面有一个是或否按钮。您可以检查用户按下了哪个按钮并相应地继续或不继续。以下是该对话框和其他对话框中的一些链接:

如果您正在运行的这段代码需要很长时间(即大于一秒),那么它可能会阻塞 wx 的主循环并导致应用程序变得无响应。如果是这种情况,那么您需要将此代码移动到一个线程中。以下文章将帮助您完成该操作:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-07
    • 2021-01-08
    • 2011-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多