Skip to content

Instantly share code, notes, and snippets.

@Bouni
Created August 6, 2024 14:49
Show Gist options
  • Save Bouni/28651a1a6cc2dd4d2749612097801f97 to your computer and use it in GitHub Desktop.
Save Bouni/28651a1a6cc2dd4d2749612097801f97 to your computer and use it in GitHub Desktop.
import wx
import wx.dataview as dv
from datamodel import PartListDataModel
import random
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="DataViewCtrl Example with Natural Sort")
panel = wx.Panel(self)
sizer = wx.BoxSizer(wx.VERTICAL)
self.dvc = dv.DataViewCtrl(
panel, style=wx.BORDER_THEME | dv.DV_ROW_LINES | dv.DV_VERT_RULES
)
self.model = PartListDataModel()
self.dvc.AssociateModel(self.model)
reference = self.dvc.AppendTextColumn(
"Reference", 0, width=50, mode=dv.DATAVIEW_CELL_EDITABLE
)
value = self.dvc.AppendTextColumn(
"Value", 1, width=150, mode=dv.DATAVIEW_CELL_EDITABLE
)
footprint = self.dvc.AppendTextColumn(
"Footprint", 2, width=150, mode=dv.DATAVIEW_CELL_EDITABLE
)
lcsc = self.dvc.AppendTextColumn(
"LCSC", 3, width=150, mode=dv.DATAVIEW_CELL_EDITABLE
)
type = self.dvc.AppendTextColumn(
"Type", 4, width=150, mode=dv.DATAVIEW_CELL_EDITABLE
)
stock = self.dvc.AppendTextColumn(
"Stock", 5, width=150, mode=dv.DATAVIEW_CELL_EDITABLE
)
bom = self.dvc.AppendIconTextColumn(
"BOM", 6, width=50, mode=dv.DATAVIEW_CELL_INERT
)
pos = self.dvc.AppendIconTextColumn(
"POS", 7, width=50, mode=dv.DATAVIEW_CELL_INERT
)
rotation = self.dvc.AppendTextColumn(
"Rotation", 8, width=50, mode=dv.DATAVIEW_CELL_EDITABLE
)
side = self.dvc.AppendTextColumn(
"Side", 9, width=50, mode=dv.DATAVIEW_CELL_EDITABLE
)
reference.SetSortable(True)
value.SetSortable(True)
footprint.SetSortable(True)
lcsc.SetSortable(True)
type.SetSortable(True)
stock.SetSortable(True)
bom.SetSortable(False)
pos.SetSortable(False)
rotation.SetSortable(True)
side.SetSortable(True)
add_button = wx.Button(panel, label="Add Entry")
add_button.Bind(wx.EVT_BUTTON, self.OnAddEntry)
modify_button = wx.Button(panel, label="Update Entry")
modify_button.Bind(wx.EVT_BUTTON, self.OnUpdateEntry)
remove_button = wx.Button(panel, label="Remove Entry")
remove_button.Bind(wx.EVT_BUTTON, self.OnRemoveEntry)
sizer.Add(self.dvc, 1, wx.EXPAND)
sizer.Add(add_button, 0, wx.ALIGN_CENTER | wx.ALL, 5)
sizer.Add(modify_button, 0, wx.ALIGN_CENTER | wx.ALL, 5)
sizer.Add(remove_button, 0, wx.ALIGN_CENTER | wx.ALL, 5)
panel.SetSizer(sizer)
self.SetSize((1200, 800))
self.Centre()
def OnAddEntry(self, event):
"""Event handler for adding a new entry."""
self.model.AddEntry(
[
f"J{len(self.model.data)}",
"Conn_01x01_Female",
"Pin_D1.0mm_L10.0mm",
"C123456",
"Extended",
str(random.randrange(10, 9999999)),
"0",
"1",
"0",
"Top",
]
)
def OnUpdateEntry(self, event):
"""Event handler for modifying a random entry."""
self.model.UpdateEntry(
2,
[
"J1",
"Conn_01x01_Female",
"Pin_D1.0mm_L10.0mm",
"C15",
"Extended",
"999999",
"1",
"1",
"270",
"Bottom",
],
)
print(self.model.data)
def OnRemoveEntry(self, event):
"""Event handler for removing the selected entry."""
item = self.dvc.GetSelection()
if item.IsOk():
self.model.RemoveEntry(item)
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame()
frame.Show()
return True
if __name__ == "__main__":
app = MyApp(False)
app.MainLoop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment