1

さて、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ドキュメントが見つかりませんでした

4

1 に答える 1

0

既に述べたように、項目を文字列としてキャストする必要があります。私は実際にアイテムコードでキャストを行うので、舞台裏で行われます。別の方法としては、wx.ListCtrl 自体をサブクラス化し、Setter をオーバーライドすることもできますが、Item クラスでそれを実行できる場合はやり過ぎだと思います。

于 2013-05-20T20:24:48.097 に答える