是否可以将数字设置为listctrl中的字符串

时间:2013-05-18 19:36:56

标签: python mysql wxpython

好的遇到了一些并发症我在Listctrl中设置了一些数字它们是SQL数据库中的数字,它们是作为字符串插入的......自动转换为数字

from wx import *
import Item as i
class ItemMain ( wx.Frame ):

    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"Items", 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 Item
        self.ite = i.Item()



        self.list = wx.ListCtrl( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize,wx.LC_REPORT )
        self.list.InsertColumn(0,"Item ID")
        self.list.InsertColumn(1,"Item Name")
        self.list.InsertColumn(2,"Item Price")
        self.list.InsertColumn(3,"Stock")
        self.list.Bind(EVT_LIST_ITEM_SELECTED,self.GetSelectedItems)
        self.list.SetMinSize( wx.Size( 500,300 ) )
        self.list.SetMaxSize( wx.Size( 1000,800 ) )
        self.refresh_list(None)

        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 Item", 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.update_entry)
        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 ItemEntry into the list"""

        self.index = 0
        self.entrydict = {}
        self.list.DeleteAllItems()
        for entry in self.ite.list_entries():
            self.list.InsertStringItem(self.index,entry.Item_ID)
            self.list.SetStringItem(self.index,1,entry.Item_Name)
            self.list.SetStringItem(self.index,2,entry.Item_Price)
            self.list.SetStringItem(self.index,3,entry.Item_Qty)
            self.entrydict[self.index] = entry
            self.index += 1


    def GetSelectedItems(self, event):
        """    Gets the selected items for the list control.
        Selection is returned as a list of selected indices,
        low to high.
        """
        selection = []
        self.index = self.list.GetFirstSelected()
        selection.append(self.index)
        while len(selection) != self.list.GetSelectedItemCount():
            self.index = self.list.GetNextSelected(self.index)
            selection.append(self.index)

        return self.index


    def add_entry(self, event):
       """Add a new entry to the Item_Qty ite"""
       # start out with blank, generic ItemEntry
       entry = i.ItemEntry("","","","0")
       self.endiag = ItemDetail(self,entry)
       self.endiag.ShowModal()           



       self.ite.add_entry(entry.Item_ID,
                            entry.Item_Name,
                            entry.Item_Price,
                            entry.Item_Qty )

       self.refresh_list(None)

    def update_entry(self, event):
       """updates the details of the currently selected entry"""
       e_id = self.index
       entry = self.ite.list_entries()[e_id]
       self.endiag2 = ItemDetailU(self , entry)
       self.endiag2.ShowModal()

       self.ite.update_entry(entry.Item,
                            entry.Item_Name,
                            entry.Item_Price,
                            entry.Item_Qty )
       self.refresh_list(None)

def __del__( self ):
    pass


class ItemDetail ( wx.Dialog ):

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

            self.entry = entry
            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.Item_ID = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
            self.Item_ID.SetMinSize( wx.Size( 200,15 ) )
            self.Item_ID.SetMaxSize( wx.Size( 500,15 ) )

            self.m_staticText6 = wx.StaticText( self, wx.ID_ANY, u"Item 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.Item_Name = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
            self.Item_Name.SetMinSize( wx.Size( 200,15 ) )
            self.Item_Name.SetMaxSize( wx.Size( 500,15 ) )

            gSizer6.Add( self.Item_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"Item Price", 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.Item_Price = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
            self.Item_Price.SetMinSize( wx.Size( 200,15 ) )
            self.Item_Price.SetMaxSize( wx.Size( 500,15 ) )

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

            self.m_staticText7 = wx.StaticText( self, wx.ID_ANY, u"Stock", 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.m_staticText8 = wx.StaticText( self, wx.ID_ANY, self.entry.Item_Qty, 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 )
            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_BUTTON, 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 closeDial(self,event):
        """Closes the Dialog"""
        self.Destroy()


    def ok(self, event):
        """sets the details of the current entry"""
        self.entry.set_details( str(self.Item_ID.GetValue()),
                            str(self.Item_Name.GetValue()),
                            str(self.Item_Price.GetValue()),
                            str(self.Item_Qty.GetValue()) )
        self.Destroy()


    def __del__( self ):
            pass                

 class ItemDetailU ( wx.Dialog ):

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

            self.entry = entry
            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.m_staticText121 = wx.StaticText( self, wx.ID_ANY, self.entry.Item_ID, wx.DefaultPosition, wx.DefaultSize, 0 )
            self.m_staticText121.Wrap( -1 )
            gSizer6.Add( self.m_staticText121, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_CENTER_HORIZONTAL, 5 )


            self.m_staticText6 = wx.StaticText( self, wx.ID_ANY, u"Item 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.Item_Name = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
            self.Item_Name.SetMinSize( wx.Size( 200,15 ) )
            self.Item_Name.SetMaxSize( wx.Size( 500,15 ) )

            gSizer6.Add( self.Item_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"Item Price", 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.Item_Price = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
            self.Item_Price.SetMinSize( wx.Size( 200,15 ) )
            self.Item_Price.SetMaxSize( wx.Size( 500,15 ) )

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

            self.m_staticText7 = wx.StaticText( self, wx.ID_ANY, u"Stock", 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.m_staticText8 = wx.StaticText( self, wx.ID_ANY, self.entry.Item_Qty, 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 )
            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_BUTTON, 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 closeDial(self,event):

        self.Destroy()

    def ok(self, event):
        self.entry.set_details( str(self.Item_ID.GetValue()),
                            str(self.Item_Name.GetValue()),
                            str(self.Item_Price.GetValue()),
                            str(self.Item_Qty.GetValue()) )
        self.Destroy()


    def __del__( self ):
            pass                


 def main():


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


 if __name__ == '__main__':
     main()

我的问题在于在listctrl中设置数字。鉴于我在设置条目时将它们转换为字符串,当调用refreshlist函数时,错误调用SetStingItem,其中值是价格可能发生在数量上坚持我插入一个字符串,并提供的不是一个字符串....任何关于如何解决这个问题的想法?找不到任何python文档来帮助我

1 个答案:

答案 0 :(得分:0)

如前所述,您需要将项目转换为字符串。我实际上会在你的物品代码中进行投射,所以它是在幕后完成的。可能有效的另一个替代方法是对wx.ListCtrl本身进行子类化并覆盖Setter,但我认为当你可以在Item类中执行它时,这是过度的。