wxPython的;如果取消所有对话框,则关闭所有对话框

时间:2018-06-18 14:38:33

标签: dialog wxpython-phoenix

我有一系列的对话框一个接一个地打开。如果我取消其中一个,我想让所有后续对话框关闭:

import wx

class MainFrame(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, -1, title='GUI Template',size=(200,200))

        panel = wx.Panel(self)

        sizer = wx.BoxSizer(wx.VERTICAL)
        btnSizer = wx.GridBagSizer(hgap=5,vgap=5)

        self.onBtn = wx.Button(panel, -1, "Start")
        self.Bind(wx.EVT_BUTTON, self.onOn, self.onBtn)

        btnSizer.Add(self.onBtn, pos=(0,0))
        sizer.Add(btnSizer, flag=wx.CENTER|wx.ALL, border = 5)

        panel.SetSizer(sizer)
        panel.Fit()


    def onOn(self,event):
        dlg = wx.SingleChoiceDialog(self, 'Would you Like to add a fruit or a veg?', '', choices=('fruit','veg'))
        if dlg.ShowModal() == wx.ID_OK:
            addType = dlg.GetStringSelection()       
            if addType == 'veg':
                dlg1 = wx.TextEntryDialog(self, 'Enter veg name')
                if dlg1.ShowModal() == wx.ID_OK:
                    grpName = str(dlg1.GetValue())
                    print grpName
                    event.Skip()

            if addType == 'fruit':
                dlg2 = wx.SingleChoiceDialog(self, 'Choose a type','', choices=('apples','oranges'))
                if dlg2.ShowModal() == wx.ID_OK:
                    group = dlg2.GetStringSelection()
                    print group                         
                    event.Skip()

                dlg3 = wx.SingleChoiceDialog(self, 'Which fruit type is this?', '', choices=('red', 'orange', 'yellow'))
                if dlg3.ShowModal() == wx.ID_OK:
                    fType = str(dlg3.GetStringSelection())
                    print fType
                    event.Skip()

                dlg4 = wx.TextEntryDialog(self, 'Enter name')
                if dlg4.ShowModal() == wx.ID_OK:
                    nName = str(dlg4.GetValue())
                    print nName
                    event.Skip()

                dlg5 = wx.TextEntryDialog(self, 'Enter address')
                if dlg5.ShowModal() == wx.ID_OK:
                    addr = str(dlg5.GetValue())
                    print addr
                    event.Skip()

            else:
                dlg.Destroy()


if __name__ == "__main__":
    app = wx.App(False)
    frame = MainFrame(None, 'GUI Template')
    frame.Show()
    app.MainLoop()

如果我点击“开始”'按钮然后突出显示' fruit'然后单击取消,不打开其他窗口。如果我点击“开始”,然后“确定”'并尝试取消下一个对话框,我必须手动取消接下来的4个对话框。

我尝试过制作一个closeDialogs函数,然后添加一个&else; closeDialogs()'每个人如果'它没有用。

我已尝试将其添加到每个对话框中:

            if dlg2.ShowModal() == wx.ID_CANCEL:
                dlg3.Destroy()
                dlg4.Destroy()
                dlg5.Destroy()

我得到UnboundLocalError:局部变量' dlg X '在分配错误之前引用。

我在Windows 10上使用Python 2.7

1 个答案:

答案 0 :(得分:0)

            if addType == 'fruit':
            dlg2 = wx.SingleChoiceDialog(self, 'Choose a type','', choices=('apples','oranges'))
            if dlg2.ShowModal() == wx.ID_OK:
                group = dlg2.GetStringSelection()
                print group                         
                event.Skip()

                dlg3 = wx.SingleChoiceDialog(self, 'Which fruit type is this?', '', choices=('red', 'orange', 'yellow'))
                if dlg3.ShowModal() == wx.ID_OK:
                    fType = str(dlg3.GetStringSelection())
                    print fType
                    event.Skip()

                    dlg4 = wx.TextEntryDialog(self, 'Enter name')
                    if dlg4.ShowModal() == wx.ID_OK:
                        nName = str(dlg4.GetValue())
                        print nName
                        event.Skip()

                        dlg5 = wx.TextEntryDialog(self, 'Enter address')
                        if dlg5.ShowModal() == wx.ID_OK:
                            addr = str(dlg5.GetValue())
                            print addr
                            event.Skip()
相关问题