Advertisement
psionman

Untitled

Aug 20th, 2023
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. import wx
  2.  
  3.  
  4. def main():
  5. # Always have main method in your app, then you can use it if you run the
  6. # module stand alone or import it elsewhere
  7. app = wx.App()
  8. frame = CompWin()
  9. frame.Show()
  10. app.MainLoop()
  11.  
  12.  
  13. class CompWin(wx.Frame): # Class names should be CamelCase
  14. # Include *args and kwargs - you might need them
  15. def __init__(self, *args, **kwargs):
  16. # Always call super on an inherited class
  17. super().__init__(None, *args, **kwargs)
  18. self.Title = 'Shortage Sheet Compiler v0.1'
  19.  
  20. # Avoid module wide variables unless constants (namespaces)
  21. self.dflist = ['Test1'], ['Test2']
  22.  
  23. panel = MainPanel(self)
  24. sizer = wx.BoxSizer(wx.VERTICAL)
  25. sizer.Add(panel)
  26. self.SetSizer(sizer)
  27. self.Center()
  28. self.Show()
  29.  
  30.  
  31. class MainPanel(wx.Panel):
  32. # Frames and panels do different things (separation of concerns) so
  33. # put your panel(s) in a different class(es)
  34. # Panels hold widgets
  35. def __init__(self, parent, *args, **kwargs):
  36. super().__init__(parent, *args, **kwargs)
  37. self.parent = parent
  38.  
  39. # Create widgets
  40. txt_update = wx.TextCtrl(self, value="Update Part:",
  41. size=(100, 20), pos=(5, 10),
  42. style=wx.TE_READONLY | wx.BORDER_NONE)
  43. self.combo_up = self.create_combobox()
  44. self.fill_combo_box()
  45. btn_refresh = wx.Button(self, label="Refresh")
  46. btn_refresh.Bind(wx.EVT_BUTTON, self.refresh_combo_box)
  47.  
  48. # Put all widgets in sizers - essential for organising layout
  49. sizer = wx.BoxSizer(wx.VERTICAL)
  50. sizer.Add(txt_update)
  51. sizer.Add(self.combo_up)
  52. sizer.Add(btn_refresh)
  53. self.SetSizer(sizer)
  54.  
  55. def create_combobox(self): # Method names and variable are not CamelCase
  56. # When a widget has a complex set up, put in separate method
  57. combo_up = wx.ListCtrl(self, wx.ID_ANY, size=(150, 125), pos=(10, 30),
  58. style=wx.LC_REPORT | wx.LC_SORT_ASCENDING)
  59. combo_up.InsertColumn(0, "Select a Part Number:", wx.LIST_FORMAT_RIGHT)
  60. combo_up.SetColumnWidth(0, 150)
  61. return combo_up
  62.  
  63. def refresh_combo_box(self, event):
  64. wx.BeginBusyCursor()
  65. self.combo_up.DeleteAllItems()
  66. self.fill_combo_box()
  67. wx.EndBusyCursor()
  68.  
  69. # Repeated code - put in a separate method
  70. def fill_combo_box(self):
  71. for part in self.parent.dflist:
  72. self.combo_up.Append(part)
  73.  
  74.  
  75. if __name__ == '__main__':
  76. # see comment in main()
  77. main()
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement