Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';在通过Pubsub使用ShowModal创建和销毁对话后,不要返回wxpython主框架_Python_Python 2.7_Wxpython_Publish Subscribe_Showmodaldialog - Fatal编程技术网

Can';在通过Pubsub使用ShowModal创建和销毁对话后,不要返回wxpython主框架

Can';在通过Pubsub使用ShowModal创建和销毁对话后,不要返回wxpython主框架,python,python-2.7,wxpython,publish-subscribe,showmodaldialog,Python,Python 2.7,Wxpython,Publish Subscribe,Showmodaldialog,我正在使用wxpython和wx.lib.pubsub编写一个应用程序。在python 2.7.3中 1-有一个带有菜单项的框架。单击此菜单时,pubsub将发布一条消息 2-此消息破坏(如果可能)并创建“第一级”对话 3-“第一级”对话有一个价值列表和一个“增值”按钮。(注意:这样的变量列表可以修改,所以我正在尝试更新此列表) 4-单击“添加值”按钮时,pubsub会发布另一条消息 5-此消息创建了一个“第二级”对话,因此可以为新变量编写一个新名称 6-此“第二级”对话中有一个“继续”按钮,它

我正在使用wxpython和wx.lib.pubsub编写一个应用程序。在python 2.7.3中

1-有一个带有菜单项的框架。单击此菜单时,pubsub将发布一条消息

2-此消息破坏(如果可能)并创建“第一级”对话

3-“第一级”对话有一个价值列表和一个“增值”按钮。(注意:这样的变量列表可以修改,所以我正在尝试更新此列表)

4-单击“添加值”按钮时,pubsub会发布另一条消息

5-此消息创建了一个“第二级”对话,因此可以为新变量编写一个新名称

6-此“第二级”对话中有一个“继续”按钮,它有两个后果:

第一个:Self.Destroy()

第二步:进入步骤2,即销毁“第一级”对话并再次创建

在这一点上,程序似乎运行良好,但是,当我完成“添加”变量到“第一级”对话时,我破坏了它,然后我无法返回到步骤1中所述的主框架

为什么会这样

所有对话都通过ShowModal()显示。但是,如果我只使用Show(),它似乎工作得很好,但是,由于程序有许多菜单和项目,showmodel()是首选。 知道为什么它与Show()一起工作,而与showmodel()不一起工作吗

如果有更简单的方法来完成我想做的任务,我将不胜感激

import wx
from wx.lib.pubsub import Publisher as pub

class itemReceiver(object):
    def __init__(self):
        pub.subscribe(self.__OnShowDialog, 'show.dialog')

    def __OnShowDialog(self, message):
        self.dlgParent = message.data[0]
        print str(self.dlgParent)
        self.valuesToShow = message.data[1]
        print self.valuesToShow
        #try to destroy dialog before creating a new one
        try:
            self.manageParametersDialog.Destroy()
        except:
            pass   
        self.manageParametersDialog = manageParamsDialog(self.dlgParent,  self.valuesToShow)
        print "ready to show first level dialogue"
        self.manageParametersDialog.ShowModal() #if .Show() instead, there is no problem

class secondaryReceiver(object):
    def __init__(self):
        pub.subscribe(self.__OnShowDialog, 'add.item')

    def __OnShowDialog(self, message):
        dlgParent = message.data[0]
        dlgGrandParent = message.data[1]
        self.variableList = message.data[2]
        editParameterDialog = editParamDlg(dlgParent, dlgGrandParent, self.variableList)
        editParameterDialog.ShowModal()

class manageParamsDialog (wx.Dialog):
    def __init__(self, parent, valueList):
        self.valueList = valueList
        self.parent = parent
        wx.Dialog.__init__(self, parent, -1, "first level dialogue", style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER) 
        sizer=wx.BoxSizer(wx.VERTICAL)
        self.optionList = wx.ListBox(self, -1, size=(200, 70), choices = valueList)
        sizer.Add(self.optionList)
        addButton = wx.Button(self, -1, 'Add New')
        self.Bind(wx.EVT_BUTTON, self.OnButton, addButton)
        sizer.Add(addButton)
        cancelButton = wx.Button(self, -1, 'Cancel')
        self.Bind(wx.EVT_BUTTON, self.OnCancel, cancelButton)
        sizer.Add(cancelButton)
        self.SetSizer(sizer)
        self.Fit()
    def OnButton (self, e):
        pub.sendMessage('add.item', [self, self.parent, self.valueList])
    def OnCancel(self,e):
        self.Destroy()

class editParamDlg(wx.Dialog):
    def __init__(self, parent, grandParent, variableList):
        self.variableList = variableList
        self.grandParent = grandParent
        wx.Dialog.__init__(self, parent, -1, "second level dialogue", style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER) 
        hboxSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.textInput = wx.TextCtrl(self, -1)
        hboxSizer.Add(self.textInput)
        addButton = wx.Button(self, -1, 'Continue')
        self.Bind(wx.EVT_BUTTON, self.OnAdd, addButton)
        hboxSizer.Add(addButton)
        cancelButton = wx.Button(self, -1, 'Cancel')
        self.Bind(wx.EVT_BUTTON, self.OnCancel, cancelButton)
        hboxSizer.Add(cancelButton)
        self.SetSizer(hboxSizer)
        self.Fit()

    def OnAdd(self, e):
        self.variableList.append(self.textInput.GetValue())
        self.Destroy()
        pub.sendMessage('show.dialog',[self.grandParent, self.variableList])
    def OnCancel(self,e):
        self.Destroy()

class ToolbarFrame(wx.Frame):
#this ToolbarFrame is the main window, with a Toolbar and a white panel below.
    def __init__(self, parent, id):    
        wx.Frame.__init__(self, parent, id, "this is a frame", size=(480, 320))
        myPanel = wx.Panel(self)
        myPanel.SetBackgroundColour("White")
        menuBar = wx.MenuBar()
        fileMenu = wx.Menu()
        menuItem = wx.MenuItem(fileMenu, -1, "menu item", "opens dialog via pubsub") 
        self.Bind(wx.EVT_MENU, self.OnMenuItem, menuItem)
        fileMenu.AppendItem(menuItem)
        menuBar.Append(fileMenu, "File")
        self.SetMenuBar(menuBar)


    def OnMenuItem(self, e):
        pub.sendMessage('show.dialog', [self, ["one", "two", "three"]])


app = wx.PySimpleApp()
frame = ToolbarFrame(parent=None, id=-1)
frame.Show()
newItemListener = itemReceiver()
editParameterListener = secondaryReceiver()
app.MainLoop()

尝试按如下方式更改第二个接收器

class secondaryReceiver(object):
    def __init__(self):
        pub.subscribe(self.__OnShowDialog, 'add.item')

    def __OnShowDialog(self, message):
        dlgParent = message.data[0]
        dlgGrandParent = message.data[1]
        self.variableList = message.data[2]
        editParameterDialog = editParamDlg(dlgParent, dlgGrandParent, self.variableList)
        editParameterDialog.ShowModal()
        #this line will not execute till the dialog closes
        self.dlgParent.optionList.SetItems(editParameterDialog.variableList)
        editParameterDialog.Destroy()
并更改editParamDlg

def OnAdd(self, e):
    self.variableList.append(self.textInput.GetValue())
    self.Close()

问题是你会从那一天起给show.modal打电话。。。这将试图破坏现有窗口,然后打开一个新窗口。。。但是旧的并没有被摧毁。。。这留下了奇怪的残余,导致了你的错误。。。实际上,您只想更新项目列表…

此描述对任何人都没有帮助我们需要看到的是一个非常小的程序,它演示了您的问题…我将尝试添加代码,但它将非常长。我不想看到一大块代码,这将导致回击(在获得帮助方面)。。。您需要将其缩减到演示问题的绝对最小示例(即使用按钮而不是菜单来触发pubsub,使用小型通用对话框而不是包含大量代码的实际对话框类),这是我可以做的最小示例,以显示我试图编写的代码,对不起,如果太长了。是的,你的代码被破坏了。。。你真的不应该这样做(我会用建议的修改来编辑我的答案)谢谢Joran,在你在第二个接收器中指出的地方加上这两行就解决了我的问题。关于editParamDlg修改,我已经在示例代码中对其进行了更改。