尽管在init函数中定义了变量,但它没有在类中定义

时间:2013-05-14 10:05:34

标签: python wxpython mysql-python

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

因此,在okD函数的CustomerDetail对话框中给出了上面的代码。它尝试调用entry,这是对Customer.py的引用,其中找到了set_details。事情是它声称它没有在类中定义,尽管我在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' 

1 个答案:

答案 0 :(得分: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__方法结束时,它将超出范围。