Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import wx
- def main():
- # Always have main method in your app, then you can use it if you run the
- # module stand alone or import it elsewhere
- app = wx.App()
- frame = CompWin()
- frame.Show()
- app.MainLoop()
- class CompWin(wx.Frame): # Class names should be CamelCase
- # Include *args and kwargs - you might need them
- def __init__(self, *args, **kwargs):
- # Always call super on an inherited class
- super().__init__(None, *args, **kwargs)
- self.Title = 'Shortage Sheet Compiler v0.1'
- # Avoid module wide variables unless constants (namespaces)
- self.dflist = ['Test1'], ['Test2']
- panel = MainPanel(self)
- sizer = wx.BoxSizer(wx.VERTICAL)
- sizer.Add(panel)
- self.SetSizer(sizer)
- self.Center()
- self.Show()
- class MainPanel(wx.Panel):
- # Frames and panels do different things (separation of concerns) so
- # put your panel(s) in a different class(es)
- # Panels hold widgets
- def __init__(self, parent, *args, **kwargs):
- super().__init__(parent, *args, **kwargs)
- self.parent = parent
- # Create widgets
- txt_update = wx.TextCtrl(self, value="Update Part:",
- size=(100, 20), pos=(5, 10),
- style=wx.TE_READONLY | wx.BORDER_NONE)
- self.combo_up = self.create_combobox()
- self.fill_combo_box()
- btn_refresh = wx.Button(self, label="Refresh")
- btn_refresh.Bind(wx.EVT_BUTTON, self.refresh_combo_box)
- # Put all widgets in sizers - essential for organising layout
- sizer = wx.BoxSizer(wx.VERTICAL)
- sizer.Add(txt_update)
- sizer.Add(self.combo_up)
- sizer.Add(btn_refresh)
- self.SetSizer(sizer)
- def create_combobox(self): # Method names and variable are not CamelCase
- # When a widget has a complex set up, put in separate method
- combo_up = wx.ListCtrl(self, wx.ID_ANY, size=(150, 125), pos=(10, 30),
- style=wx.LC_REPORT | wx.LC_SORT_ASCENDING)
- combo_up.InsertColumn(0, "Select a Part Number:", wx.LIST_FORMAT_RIGHT)
- combo_up.SetColumnWidth(0, 150)
- return combo_up
- def refresh_combo_box(self, event):
- wx.BeginBusyCursor()
- self.combo_up.DeleteAllItems()
- self.fill_combo_box()
- wx.EndBusyCursor()
- # Repeated code - put in a separate method
- def fill_combo_box(self):
- for part in self.parent.dflist:
- self.combo_up.Append(part)
- if __name__ == '__main__':
- # see comment in main()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement