【问题标题】:Can't go back to wxpython main frame after creating and destroying Dialogue using ShowModal via Pubsub通过 Pubsub 使用 ShowModal 创建和销毁 Dialogue 后无法返回 wxpython 主框架
【发布时间】:2014-11-18 14:08:52
【问题描述】:

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

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

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

3-“第一级”对话框有一个值列表和一个“添加值”按钮。 (注意:此类变量列表可以修改,因此我正在尝试更新此列表)

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

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

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

第一个:Self.Destroy();

第二个:进入第2步,即销毁“第一级”对话并重新创建。

到目前为止,该程序似乎运行良好,但是,当我将变量“添加”到“第一级”对话框中时,我将其销毁,然后我无法返回步骤 1 中所述的主框架。

为什么会这样?

所有对话都通过 ShowModal() 显示。但是,如果我只使用 Show() 它似乎可以正常工作,但是由于该程序有很多菜单和项目,所以 ShowModal() 是首选。 知道为什么它适用于 Show() 但不适用于 ShowModal()?

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

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

【问题讨论】:

  • 这个描述对任何人都没有帮助我们需要看到的是一个非常小的程序来展示你的问题......
  • 我会尝试添加代码,但会很长
  • 我不想看到一大段代码,这会适得其反(就获得您的帮助而言)...您需要将其缩减为演示问题(即使用按钮而不是菜单来触发 pubsub,使用小型通用对话框而不是具有大量代码的实际对话框类)
  • 这是我为了展示我正在尝试编码的内容而能做的最低限度,如果太长,请见谅。
  • 是的,你的代码被破坏了......你真的不应该那样做(我会用建议的修改来编辑我的答案)

标签: python python-2.7 wxpython publish-subscribe showmodaldialog


【解决方案1】:

尝试如下更改secondaryReciever

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

问题是你会从那个 OnAdd 调用 show.modal ...它会尝试破坏现有的窗口然后打开一个新的...但是旧的没有被破坏...这留下了奇怪的残余这导致了您的错误...实际上您要做的就是更新项目列表...

【讨论】:

  • 谢谢 Joran,在您在 secondaryReceiver 中指定的位置之后添加这两行解决了我的问题。关于editParamDlg的修改我已经在示例代码中改过了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-03
  • 2010-11-14
  • 2014-02-02
  • 1970-01-01
  • 1970-01-01
  • 2018-05-03
  • 1970-01-01
相关资源
最近更新 更多