【问题标题】:wxPython; getting all dialogs to close if one is cancelledwxPython;如果取消一个对话框,则关闭所有对话框
【发布时间】:2018-11-27 10:42:18
【问题描述】:

我有一系列依次打开的对话框。如果我取消其中一个,我希望关闭所有后续对话框:

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

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

我尝试制作一个 closeDialogs 函数并在每个“if”中添加一个“else: closeDialogs()”,但没有成功。

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

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

我得到 UnboundLocalError: local variable 'dlgX' referenced before assignment error.

我在 Windows 10 上使用 Python 2.7

【问题讨论】:

    标签: dialog wxpython


    【解决方案1】:
                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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-09
      • 2023-03-15
      • 1970-01-01
      • 2015-12-30
      • 1970-01-01
      • 2018-01-04
      • 1970-01-01
      相关资源
      最近更新 更多