wxPython - 检查空文本对话框

时间:2021-12-23 07:11:45

I have some python code, using wxpython, that will do the following:

我有一些使用wxpython的python代码,它将执行以下操作:

  1. ask the user for a number using a wxpython dialog box
  2. 使用wxpython对话框询问用户输入的数字

  3. check if that number is currently in use, if it is the box appears asking for a number again
  4. 检查当前是否正在使用该号码,如果该号码出现则再次询问号码

  5. Once an unused number has been entered, a new dialog will appear asking for a name. If in use, it will ask again
  6. 输入未使用的号码后,将出现一个新的对话框,询问名称。如果在使用中,它会再次询问

  7. Both the new number and name are stored for later use
  8. 新数字和名称都存储起来供以后使用

The code looks something like this:

代码看起来像这样:

while True:
            codeNumberPrompt = wx.TextEntryDialog(self, "New code #: ", "Add New Code", str(len(self.codes)), style=wx.OK)
            codeNumberPrompt.ShowModal()
            self.codeNumber = codeNumberPrompt.GetValue()

            #check if number is in use
            if int(self.codeNumber) not in self.numbersInUse:
                #new code name input
                while True:
                    codeNamePrompt = wx.TextEntryDialog(self, "New code name: ", "Add New Code", style=wx.OK)
                    codeNamePrompt.ShowModal()
                    self.codeName = codeNamePrompt.GetValue()

                    #check if name is in use
                    if self.codeName.lower() not in self.namesInUse:
                        break

                    wx.MessageBox('Code name is currently in use', 'Warning', wx.OK | wx.ICON_INFORMATION)
                break
            else:
                wx.MessageBox('Code number is currently in use', 'Warning', wx.OK | wx.ICON_INFORMATION)

My first question, is there a better/cleaner way to do this?

我的第一个问题是,有更好/更清洁的方法吗?

My second question (where I get stuck): I need to be able to handle some exception cases. For example:

我的第二个问题(我陷入困境):我需要能够处理一些异常情况。例如:

  • if the user X's out of the first dialog, I dont want the second dialog box to show up and I want to proceed as if nothing happened (i.e. doesnt try to store the number or name values)
  • 如果用户X出于第一个对话框,我不希望第二个对话框出现,我想继续进行,好像什么也没发生(即不试图存储数字或名称值)

  • if the user leaves the number or name dialog entry blank, I want to ask them for that info again
  • 如果用户将数字或名称对话框条目留空,我想再次询问他们该信息

I cant seem to find out how wxpython handles the X button in dialog boxes. Currently, if the user leaves a default value in the first (number) box, and then X's, the second box (name) will still show up

我似乎无法找出wxpython如何处理对话框中的X按钮。目前,如果用户在第一个(数字)框中保留默认值,然后是X,则第二个框(名称)仍将显示

1 个解决方案

#1


You should be able to catch the wx.CLOSE event and prevent a dialog from closing. However, I believe you would be better off using a wizard-type approach. In the first step, you would check the number and then hit Next. Then you would accept the name and close out of the dialog. I feel like that would be a more natural flow for the user.

您应该能够捕获wx.CLOSE事件并阻止关闭对话框。但是,我相信你最好使用向导式方法。在第一步中,您将检查数字,然后单击下一步。然后你会接受名称并关闭对话框。我觉得这对用户来说是更自然的流程。

I put together a fun little example, but you will note that I didn't save the data in any meaningful way. You'll want to add that yourself:

我把一个有趣的小例子放在一起,但你会注意到我没有以任何有意义的方式保存数据。你想要自己添加:

import wx

########################################################################
class MyDialog(wx.Dialog):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Dialog.__init__(self, None, title="Information")

        self.numbers = [1, 2, 3]
        self.names = ['mike', 'nick', 'george']

        #--------------------------------------------------
        # create the number panel
        self.number_panel = wx.Panel(self)
        self.number_text = wx.TextCtrl(self.number_panel)
        self.number_text.SetFocus()
        next_btn = wx.Button(self.number_panel, label='Next')
        next_btn.Bind(wx.EVT_BUTTON, self.onNext)
        panel_sizer = wx.BoxSizer(wx.VERTICAL)
        panel_sizer.Add(self.number_text, 0, wx.ALL|wx.EXPAND, 5)
        panel_sizer.Add(next_btn, 0, wx.ALL, 5)
        self.number_panel.SetSizer(panel_sizer)

        #--------------------------------------------------
        # create the name panel
        self.name_panel = wx.Panel(self)
        self.name_panel.Hide()
        self.name_text = wx.TextCtrl(self.name_panel)

        ok_btn = wx.Button(self.name_panel, label='OK')
        ok_btn.Bind(wx.EVT_BUTTON, self.onOK)

        name_sizer = wx.BoxSizer(wx.VERTICAL)
        name_sizer.Add(self.name_text, 0, wx.ALL, 5)
        name_sizer.Add(ok_btn, 0, wx.ALL, 5)
        self.name_panel.SetSizer(name_sizer)

        #--------------------------------------------------
        # layout panels
        self.my_sizer = wx.BoxSizer(wx.VERTICAL)
        self.my_sizer.Add(self.number_panel, 1, wx.EXPAND)
        self.my_sizer.Add(self.name_panel, 1, wx.EXPAND)
        self.SetSizer(self.my_sizer)

        #--------------------------------------------------
        self.ShowModal()
        wx.CallAfter(self.my_sizer.Layout)

    #----------------------------------------------------------------------
    def onNext(self, event):
        """"""
        number = int(self.number_text.GetValue())
        if number in self.numbers:
            wx.MessageBox('Code number is currently in use', 
                          'Warning', wx.OK | wx.ICON_INFORMATION)
            self.number_text.SetFocus()
            return
        else:
            self.my_sizer.Detach(self.number_panel)
            self.name_panel.Show()
            self.Layout()
            self.numbers.append(number)

    #----------------------------------------------------------------------
    def onOK(self, event):
        """"""
        name = self.name_text.GetValue()
        if name.lower() in self.names:
            wx.MessageBox('Code name is currently in use', 
                          'Warning', wx.OK | wx.ICON_INFORMATION)
            return
        else:
            self.names.append(name)
            self.Close()


########################################################################
class MyPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)

        info_button = wx.Button(self, label="Get Info")
        info_button.Bind(wx.EVT_BUTTON, self.onGetInfo)

    #----------------------------------------------------------------------
    def onGetInfo(self, event):
        """"""
        dlg = MyDialog()
        dlg.Destroy()


########################################################################
class MyFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title='Demo')
        panel = MyPanel(self)
        self.Show()

if __name__ == '__main__':
    app = wx.App(False)
    frame = MyFrame()
    app.MainLoop()

#1


You should be able to catch the wx.CLOSE event and prevent a dialog from closing. However, I believe you would be better off using a wizard-type approach. In the first step, you would check the number and then hit Next. Then you would accept the name and close out of the dialog. I feel like that would be a more natural flow for the user.

您应该能够捕获wx.CLOSE事件并阻止关闭对话框。但是,我相信你最好使用向导式方法。在第一步中,您将检查数字,然后单击下一步。然后你会接受名称并关闭对话框。我觉得这对用户来说是更自然的流程。

I put together a fun little example, but you will note that I didn't save the data in any meaningful way. You'll want to add that yourself:

我把一个有趣的小例子放在一起,但你会注意到我没有以任何有意义的方式保存数据。你想要自己添加:

import wx

########################################################################
class MyDialog(wx.Dialog):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Dialog.__init__(self, None, title="Information")

        self.numbers = [1, 2, 3]
        self.names = ['mike', 'nick', 'george']

        #--------------------------------------------------
        # create the number panel
        self.number_panel = wx.Panel(self)
        self.number_text = wx.TextCtrl(self.number_panel)
        self.number_text.SetFocus()
        next_btn = wx.Button(self.number_panel, label='Next')
        next_btn.Bind(wx.EVT_BUTTON, self.onNext)
        panel_sizer = wx.BoxSizer(wx.VERTICAL)
        panel_sizer.Add(self.number_text, 0, wx.ALL|wx.EXPAND, 5)
        panel_sizer.Add(next_btn, 0, wx.ALL, 5)
        self.number_panel.SetSizer(panel_sizer)

        #--------------------------------------------------
        # create the name panel
        self.name_panel = wx.Panel(self)
        self.name_panel.Hide()
        self.name_text = wx.TextCtrl(self.name_panel)

        ok_btn = wx.Button(self.name_panel, label='OK')
        ok_btn.Bind(wx.EVT_BUTTON, self.onOK)

        name_sizer = wx.BoxSizer(wx.VERTICAL)
        name_sizer.Add(self.name_text, 0, wx.ALL, 5)
        name_sizer.Add(ok_btn, 0, wx.ALL, 5)
        self.name_panel.SetSizer(name_sizer)

        #--------------------------------------------------
        # layout panels
        self.my_sizer = wx.BoxSizer(wx.VERTICAL)
        self.my_sizer.Add(self.number_panel, 1, wx.EXPAND)
        self.my_sizer.Add(self.name_panel, 1, wx.EXPAND)
        self.SetSizer(self.my_sizer)

        #--------------------------------------------------
        self.ShowModal()
        wx.CallAfter(self.my_sizer.Layout)

    #----------------------------------------------------------------------
    def onNext(self, event):
        """"""
        number = int(self.number_text.GetValue())
        if number in self.numbers:
            wx.MessageBox('Code number is currently in use', 
                          'Warning', wx.OK | wx.ICON_INFORMATION)
            self.number_text.SetFocus()
            return
        else:
            self.my_sizer.Detach(self.number_panel)
            self.name_panel.Show()
            self.Layout()
            self.numbers.append(number)

    #----------------------------------------------------------------------
    def onOK(self, event):
        """"""
        name = self.name_text.GetValue()
        if name.lower() in self.names:
            wx.MessageBox('Code name is currently in use', 
                          'Warning', wx.OK | wx.ICON_INFORMATION)
            return
        else:
            self.names.append(name)
            self.Close()


########################################################################
class MyPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)

        info_button = wx.Button(self, label="Get Info")
        info_button.Bind(wx.EVT_BUTTON, self.onGetInfo)

    #----------------------------------------------------------------------
    def onGetInfo(self, event):
        """"""
        dlg = MyDialog()
        dlg.Destroy()


########################################################################
class MyFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title='Demo')
        panel = MyPanel(self)
        self.Show()

if __name__ == '__main__':
    app = wx.App(False)
    frame = MyFrame()
    app.MainLoop()