gpt4 book ai didi

python - 是否可以在 listctrl 中将数字设置为字符串

转载 作者:行者123 更新时间:2023-11-30 23:18:17 24 4
gpt4 key购买 nike

好吧,在我的 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 中设置数字。考虑到我在设置条目时将它们转换为字符串,当调用刷新列表函数时,错误调用 SetStingItem,其中值为价格 Likely to happen to quantity also 坚持我插入一个字符串,而提供的不是一个字符串……关于如何解决这个问题的任何想法?找不到任何 python 文档来帮助我

最佳答案

如前所述,您需要将项目转换为字符串。我实际上会在您的 Item 代码中进行转换,所以它是在幕后完成的。另一种可能可行的替代方法是对 wx.ListCtrl 本身进行子类化并覆盖 Setter,但我认为如果您可以在 Item 类中这样做就太过分了。

关于python - 是否可以在 listctrl 中将数字设置为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16628238/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com