我可以将第一个面板中的“确定”按钮绑定到运行的功能上,并检查是否正确输入了id,是否正确,是否出现了下一个面板

时间:2019-03-21 18:29:54

标签: wxpython

import wx
class MainFrame (wx.Frame):
    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 800,600 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
        bSizer1 = wx.BoxSizer( wx.VERTICAL )
        self.SetSizer( bSizer1 )
        self.Layout()

        self.Centre( wx.BOTH )
    def __del__( self ):
        pass
class first_panel ( wx.Panel ):
    def __init__( self, parent ):
        wx.Panel.__init__ ( self, parent, id = wx.ID_ANY, pos = wx.DefaultPosition, size = wx.Size( 800,600 ), style = wx.TAB_TRAVERSAL )
        sizer = wx.GridBagSizer(5, 5)
        greetings = wx.StaticText(self, label='Welcome to 24-hour ATM service')
        sizer.Add(greetings, pos=(0, 0), flag=wx.TOP|wx.LEFT|wx.BOTTOM, border=15)
        line = wx.StaticLine(self)
        sizer.Add(line, pos=(1, 0), span=(1, 5), flag=wx.EXPAND|wx.BOTTOM, border=10)
        customerIdText = wx.StaticText(self, label="Customer Id")
        sizer.Add(customerIdText, pos=(3, 0), flag=wx.LEFT|wx.TOP, border=10)
        customerIdInput = wx.TextCtrl(self)
        sizer.Add(customerIdInput, pos=(3, 1), span=(1, 3), flag=wx.TOP|wx.EXPAND, border=5)
        okButton = wx.Button(self, label="Ok")
        sizer.Add(okButton, pos=(4, 3), span=(1, 1))
        okButton.Bind(wx.EVT_BUTTON, self.OnOkClicked)
        cancelButton = wx.Button(self, label="Cancel")

1 个答案:

答案 0 :(得分:0)

您可以使用多个类,也可以在一个类中执行任务。
由于该过程似乎是顺序的,因此从一开始就可以更轻松地在单个类中完成所有操作。
希望您正在寻找类似的东西。

import wx

choices=["Withdraw Cash","Make a deposit","Check Balance"]

class Example(wx.Frame):

    def __init__(self, parent, title):
        super(Example, self).__init__(parent, title=title, size=(750,-1))
        self.Customer_panel()
        self.Show()

    def Customer_panel(self):
        self.panel = wx.Panel(self, -1)
        sizer = wx.BoxSizer(wx.VERTICAL)
        text = wx.StaticText(self.panel, label="Customer Id:")
        sizer.Add(text, 0, flag=wx.ALL, border=5)
        self.customer_id = wx.TextCtrl(self.panel)
        sizer.Add(self.customer_id, 0, flag=wx.ALL, border=5)
        next = wx.Button(self.panel, label="Login")
        sizer.Add(next, 0, flag=wx.ALL, border=5)
        self.panel.SetSizerAndFit(sizer)
        next.Bind(wx.EVT_BUTTON, self.OnNext_1)
        self.customer_id.SetFocus()

    def OnNext_1(self, event):
        self.CustId = self.customer_id.GetValue()
        if self.CustId != "1442":
            wx.MessageBox("Incorrect Customer Id","Error", wx.OK | wx.ICON_ERROR)
            self.customer_id.SetValue('')
            self.customer_id.SetFocus()
            return
        self.panel.Destroy()
        self.Transaction_type_panel()

    def Transaction_type_panel(self):
        self.panel = wx.Panel(self, -1)
        sizer = wx.BoxSizer(wx.VERTICAL)
        text = wx.StaticText(self.panel, label="Customer Id: "+str(self.CustId))
        sizer.Add(text, 0, flag=wx.ALL, border=5)
        self.trans_type = wx.ComboBox(self.panel, -1, "Transaction type", choices=choices)
        sizer.Add(self.trans_type, 0, flag=wx.ALL, border=5)
        next = wx.Button(self.panel, label="Next")
        sizer.Add(next, 0, flag=wx.ALL, border=5)
        back = wx.Button(self.panel, label="Back")
        sizer.Add(back, 0, flag=wx.ALL, border=5)
        exit = wx.Button(self.panel, label="Exit")
        sizer.Add(exit, 0, flag=wx.ALL, border=5)
        self.panel.SetSizerAndFit(sizer)
        next.Bind(wx.EVT_BUTTON, self.OnNext_2)
        back.Bind(wx.EVT_BUTTON, self.OnBack_2)
        exit.Bind(wx.EVT_BUTTON, self.OnExit_2)
        self.trans_type.SetSelection(0)

    def OnNext_2(self, event):
        self.Type = self.trans_type.GetSelection()
        self.panel.Destroy()
        self.Transaction_panel()

    def OnBack_2(self, event):
        self.panel.Destroy()
        self.Customer_panel()

    def OnExit_2(self, event):
        self.Destroy()

    def Transaction_panel(self):
        self.panel = wx.Panel(self, -1)
        sizer = wx.BoxSizer(wx.VERTICAL)
        text = wx.StaticText(self.panel, label="Customer Id: "+str(self.CustId))
        sizer.Add(text, 0, flag=wx.ALL, border=5)
        text2 = wx.StaticText(self.panel, label="You chose to "+choices[self.Type])
        sizer.Add(text2, 0, flag=wx.ALL, border=5)
        exit = wx.Button(self.panel, label="Exit")
        sizer.Add(exit, 0, flag=wx.ALL, border=5)
        back = wx.Button(self.panel, label="Back")
        sizer.Add(back, 0, flag=wx.ALL, border=5)
        self.panel.SetSizerAndFit(sizer)
        exit.Bind(wx.EVT_BUTTON, self.OnExit_3)
        back.Bind(wx.EVT_BUTTON, self.OnBack_3)

    def OnExit_3(self, event):
        self.Destroy()

    def OnBack_3(self, event):
        self.panel.Destroy()
        self.Transaction_type_panel()

app = wx.App()
Example(None, title='Sequential Panels')
app.MainLoop()