0
from wx import *
import Customer as c

class CustomerMain ( wx.Frame ):

        def __init__( self, parent ):
            wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"Customers", pos = wx.DefaultPosition, size = wx.Size( 517,486 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )

            self.SetSizeHintsSz( wx.Size( -1,-1 ), wx.DefaultSize )

            bSizer3 = wx.BoxSizer( wx.VERTICAL )


            # Create a customer
            self.cust = c.Customer()

            self.list = wx.ListCtrl( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.LC_ICON )
            self.list.InsertColumn(0,'ID', width = 140)
            self.list.InsertColumn(1,'First Name', width = 140)
            self.list.InsertColumn(2,'Last Name', width = 140)
            self.list.SetMinSize( wx.Size( 500,300 ) )
            self.list.SetMaxSize( wx.Size( 1000,800 ) )



            bSizer3.Add( self.list, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 5 )

            bSizer4 = wx.BoxSizer( wx.HORIZONTAL )

            self.m_button20 = wx.Button( self, wx.ID_ANY, u"Add Customer", wx.DefaultPosition, wx.DefaultSize, 0 )
            self.m_button20.Bind(wx.EVT_BUTTON, self.add_entry)
            bSizer4.Add( self.m_button20, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5 )

            self.m_button21 = wx.Button( self, wx.ID_ANY, u"Update Record", wx.DefaultPosition, wx.DefaultSize, 0 )
            self.m_button21.Bind(wx.EVT_BUTTON, self.add_entry)
            self.refresh_list(None)
            bSizer4.Add( self.m_button21, 1, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )

            bSizer3.Add( bSizer4, 1, wx.EXPAND, 5 )

            self.SetSizer( bSizer3 )
            self.Layout()

            self.Centre( wx.BOTH )
            self.Show()

        def refresh_list(self, event):
            """Place the names of each CustomerEntry into the list"""

            index =0
            self.entryDict = {}
            for entry in self.cust.list_entries():
                self.list.InsertStringItem(index,entry.Cust_ID)
                self.list.SetStringItem(index,1,entry.L_Name)
                self.list.SetStringItem(index,2,entry.F_Name)
                index += 1

##            self.list.RefreshItems(self)
        def add_entry(self, event):
           """Add a new entry to the Address cust"""
           # start out with blank, generic CustomerEntry
           entry = c.CustomerEntry("", "", "", "","" )
           self.endiag = CustomerDetail(self , entry)
           self.endiag.Show()

           # now we got details in new_entry, add it
           self.cust.add_entry(entry.Cust_ID,entry.F_Name, entry.L_Name, entry.Address,entry.Cust_Stat)

##           self.refresh_list()

        def show_detail(self, event):
           e_id = int(self.list.curselection()[0])
           entry = self.cust.list_entries()[e_id]
           CustomerDetail(entry)
           self.refresh_list()

def __del__( self ):
    pass


class CustomerDetail ( wx.Dialog ):

        def __init__( self, parent,entry ):
                wx.Dialog.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 690,500 ), style = wx.DEFAULT_DIALOG_STYLE )

                self.SetSizeHintsSz( wx.Size( 500,500 ), wx.DefaultSize )

                bSizer6 = wx.BoxSizer( wx.VERTICAL )

                gSizer6 = wx.GridSizer( 2, 2, 0, 0 )

                self.m_staticText5 = wx.StaticText( self, wx.ID_ANY, u"ID", wx.DefaultPosition, wx.DefaultSize, 0 )
                self.m_staticText5.Wrap( -1 )
                gSizer6.Add( self.m_staticText5, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 5 )

                self.Cust_ID = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
                self.Cust_ID.SetMinSize( wx.Size( 200,15 ) )
                self.Cust_ID.SetMaxSize( wx.Size( 500,15 ) )

                gSizer6.Add( self.Cust_ID, 1, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.ALL|wx.EXPAND, 5 )

                self.m_staticText6 = wx.StaticText( self, wx.ID_ANY, u"First Name", wx.DefaultPosition, wx.DefaultSize, 0 )
                self.m_staticText6.Wrap( -1 )
                gSizer6.Add( self.m_staticText6, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 5 )

                self.F_Name = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
                self.F_Name.SetMinSize( wx.Size( 200,15 ) )
                self.F_Name.SetMaxSize( wx.Size( 500,15 ) )

                gSizer6.Add( self.F_Name, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_CENTER_HORIZONTAL|wx.EXPAND, 5 )

                self.m_staticText7 = wx.StaticText( self, wx.ID_ANY, u"Last Name", wx.DefaultPosition, wx.DefaultSize, 0 )
                self.m_staticText7.Wrap( -1 )
                gSizer6.Add( self.m_staticText7, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 5 )

                self.L_Name = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
                self.L_Name.SetMinSize( wx.Size( 200,15 ) )
                self.L_Name.SetMaxSize( wx.Size( 500,15 ) )

                gSizer6.Add( self.L_Name, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.EXPAND|wx.ALIGN_CENTER_VERTICAL, 5 )

                self.m_staticText8 = wx.StaticText( self, wx.ID_ANY, u"Address", wx.DefaultPosition, wx.DefaultSize, 0 )
                self.m_staticText8.Wrap( -1 )
                gSizer6.Add( self.m_staticText8, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 5 )

                self.Address = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
                self.Address.SetMinSize( wx.Size( 200,15 ) )
                self.Address.SetMaxSize( wx.Size( 500,15 ) )

                gSizer6.Add( self.Address, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, 5 )

                self.m_staticText12 = wx.StaticText( self, wx.ID_ANY, u"Status", wx.DefaultPosition, wx.DefaultSize, 0 )
                self.m_staticText12.Wrap( -1 )
                gSizer6.Add( self.m_staticText12, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 5 )


                stat_choices = ["Active","Inactive","Archived"]

                self.Cust_Stat = wx.ComboBox( self, wx.ID_ANY, u"select option!", wx.DefaultPosition, wx.DefaultSize, stat_choices, 0 )
                self.Cust_Stat.SetMinSize( wx.Size( 200,15 ) )
                self.Cust_Stat.SetMaxSize( wx.Size( 300,15 ) )
                self.Cust_Stat.Bind(wx.EVT_COMBOBOX, self.OnSelect)
                gSizer6.Add( self.Cust_Stat, 0, wx.ALL, 5 )

                bSizer6.Add( gSizer6, 1, wx.EXPAND, 5 )

                bSizer13 = wx.BoxSizer( wx.HORIZONTAL )

                self.m_button24 = wx.Button( self, wx.ID_ANY, u"ok", wx.DefaultPosition, wx.DefaultSize, 0 )
                self.m_button24.Bind(wx.EVT_BUTTON, self.ok)

                bSizer13.Add( self.m_button24, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5 )

                self.m_button25 = wx.Button( self, wx.ID_ANY, u"cancel", wx.DefaultPosition, wx.DefaultSize, 0 )
                self.m_button25.Bind(wx.EVT_CLOSE, self.closeDial)

                bSizer13.Add( self.m_button25, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5 )

                bSizer6.Add( bSizer13, 1, wx.EXPAND, 5 )

                self.SetSizer( bSizer6 )
                self.Layout()

                self.Centre( wx.BOTH )

        def OnSelect (self, event):
            self.Cust_Stat.GetValue()

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


        def ok(self, event):
            self.entry.set_details( str(self.Cust_ID.get()),
                                str(self.F_Name.get()),
                                str(self.L_Name.get()),
                                str(self.Address.get()),
                                str(self.Cust_Stat.get()) )
            self.closeDial

        def __del__( self ):
                pass                
def main():

    ex = wx.App()
    CustomerMain(None)
    ex.MainLoop()    


if __name__ == '__main__':
    main()

因此,在 CustomerDetail 对话框中给出上面的代码......在 ok 函数中。它尝试调用 entry,这是对找到 set_details 的 Customer.py 的引用。事情是它声称它没有在类中定义,尽管我将它设置在 init 函数中并将其定义为客户对象的处理程序......有什么想法吗?

追溯

Traceback (most recent call last):
File "C:\Users\Owner\Desktop\sad pROG\customer_app.py", line 173, in ok 
    self.entry.set_details( str(self.Cust_ID.get()),
AttributeError: 'CustomerDetail' object has no attribute 'entry' 
4

1 回答 1

1

看起来您entry作为参数传入CustomerDetail.__init__,但没有在对象上设置它。

你需要做这样的事情......

class CustomerDetail ( wx.Dialog ):
    def __init__( self, parent,entry ):
        wx.Dialog.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 690,500 ), style = wx.DEFAULT_DIALOG_STYLE )
        self.entry = entry
        # <rest of code here>

__init__...将变量存储为类属性,否则方法结束时它将超出范围。

于 2013-05-14T10:19:27.857 回答