wxPython ComboBox Demo

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
vegaseat vegaseat is offline Offline Oct 21st, 2005, 6:32 pm |
0
The wx.ComboBox is a combination of an editbox and a (dropdown) listbox. It let's the user slect an item from the listbox and puts it in the editbox. In this snippet we use two comboboxes, each used to select an area unit of measurement. Once selected, an area value can be converted from a unit like acres to another unit, for instance squaremiles. Additional wxPython widgets like wx.Frame, wx.Panel, wx.ToolTip, wx.StaticText, wx.TextCtrl, and wx.Button are part of this demonstration. The layout is done entirely with upper left corner position (x, y) tuples and size (width, height) tuples. This demo is supposed to illustrate the layout too, so I used plenty of key-words like pos=wx.Point(x, y) and size=wx.Size(width, height).
Quick reply to this message  
Python Syntax
  1. # use wxPython's wx.ComboBox() to select different units of measurement
  2. # then convert the area associated with the selections
  3. # tested with Python24 and wxPython26 vegaseat 21oct2005
  4.  
  5. import wx
  6.  
  7. class MyPanel(wx.Panel):
  8. """
  9. class MyPanel creates a panel with 2 comboboxes and more, inherits wx.Panel
  10. (putting your components/widgets on a panel gives additional versatility)
  11. """
  12. def __init__(self, parent, id):
  13. # no pos and size given, so panel defaults to fill the parent frame
  14. wx.Panel.__init__(self, parent, id)
  15. self.SetBackgroundColour((255,228,196)) # bisque
  16.  
  17. # no size given, so the text determines the needed label size
  18. wx.StaticText(self, -1, "convert from:", (10, 10))
  19. # create a combo box to select units of measurement to convert from
  20. self.combo1 = wx.ComboBox(self, -1, value=areaList[0], pos=wx.Point(10, 30),
  21. size=wx.Size(120, 150), choices=areaList)
  22. # optional tooltip
  23. self.combo1.SetToolTip(wx.ToolTip("select unit from dropdown-list"))
  24.  
  25. wx.StaticText(self, -1, "convert to:", pos=wx.Point(150, 10))
  26. # create a combo box to select units of measurement to convert to
  27. self.combo2 = wx.ComboBox(self, -1, value=areaList[0], pos=wx.Point(150, 30),
  28. size=wx.Size(120, 150), choices=areaList)
  29.  
  30. wx.StaticText(self, -1, "value to convert:", pos=wx.Point(10, 70))
  31. self.edit1 = wx.TextCtrl(self, -1, value="1", pos=wx.Point(10, 90), size=wx.Size(175, 25))
  32. self.edit1.SetBackgroundColour((255,255,197)) # suds yellow
  33.  
  34. self.button1 = wx.Button(self, -1, label="Do the Conversion ...",
  35. pos=wx.Point(10, 130), size=wx.Size(175, 28))
  36. # respond to button click event
  37. self.button1.Bind(wx.EVT_BUTTON, self.button1Click, self.button1)
  38.  
  39. wx.StaticText(self, -1, "result:", (10, 170))
  40. self.edit2 = wx.TextCtrl(self, -1, value="", pos=wx.Point(10, 190), size=wx.Size(350, 25))
  41. self.edit2.SetBackgroundColour((217,255,219)) # vegaseat green
  42.  
  43. def button1Click(self,event):
  44. """Conversion button has been clicked"""
  45. unit1 = self.combo1.GetValue()
  46. unit2 = self.combo2.GetValue()
  47. x = float(self.edit1.GetValue())
  48. y = convertArea(x, unit1, unit2)
  49. if y < 0.001:
  50. str1 = "%f %s = %0.12f %s" % (x, unit1, y, unit2) # very small y
  51. elif y > 1000:
  52. str1 = "%f %s = %0.3f %s" % (x, unit1, y, unit2) # very large y
  53. else:
  54. str1 = "%f %s = %f %s" % (x, unit1, y, unit2) # 6 decimals is default
  55. self.edit2.SetValue(str1)
  56.  
  57.  
  58. def convertArea(x, unit1, unit2):
  59. """convert area x of unit1 to area of unit2 and return area, on error return False"""
  60. if (unit1 in areaD) and (unit2 in areaD):
  61. factor1 = areaD[unit1]
  62. factor2 = areaD[unit2]
  63. return factor2*x/factor1
  64. else:
  65. return False
  66.  
  67. #create an empty dictionary
  68. areaD = {}
  69. # populate dictionary with units and conversion factors relative to sqmeter = 1.0
  70. # this minimizes the total number of conversion factors
  71. areaD['sqmeter'] = 1.0
  72. areaD['sqmillimeter'] = 1000000.0
  73. areaD['sqcentimeter'] = 10000.0
  74. areaD['sqkilometer'] = 0.000001
  75. areaD['hectare'] = 0.0001
  76. areaD['sqinch'] = 1550.003
  77. areaD['sqfoot'] = 10.76391
  78. areaD['sqyard'] = 1.19599
  79. areaD['acre'] = 0.0002471054
  80. areaD['sqmile'] = 0.0000003861022
  81.  
  82. # create a sorted list for the combo boxes
  83. areaList = sorted(areaD.keys())
  84.  
  85.  
  86. app = wx.PySimpleApp()
  87. # create a window/frame, no parent, -1 is default ID, title, size
  88. frame = wx.Frame(None, -1, "Convert Area ...", size = (400, 300))
  89. # call the derived class, -1 is default ID, can also use wx.ID_ANY
  90. MyPanel(frame,-1)
  91. # show the frame
  92. frame.Show(True)
  93. # start the event loop
  94. app.MainLoop()

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC