Starting wxPython (GUI code)

Reply

Join Date: Aug 2005
Posts: 1,514
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 168
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Starting wxPython (GUI code)

 
1
  #11
Jun 9th, 2008
To draw simple shapes like lines, circles, rectangles you have to use the wx.PaintDC surface as the canvas. Here are some basic examples:
  1. # the wx.PaintDC surface is wxPython's canvas
  2. # draw a line on this canvas
  3. # use help(wx.PaintDC) to get more info
  4.  
  5. import wx
  6.  
  7. class DrawPanel(wx.Panel):
  8. """draw a line on a panel's wx.PaintDC surface/canvas"""
  9. def __init__(self, parent):
  10. wx.Panel.__init__(self, parent, -1)
  11. # bind the panel to the paint event
  12. wx.EVT_PAINT(self, self.onPaint)
  13.  
  14. def onPaint(self, event=None):
  15. # this is the wx drawing surface/canvas
  16. dc = wx.PaintDC(self)
  17. dc.Clear()
  18. # sets pen color and width
  19. dc.SetPen(wx.Pen("blue", 1))
  20. x1 = 20
  21. y1 = 10
  22. x2 = 500
  23. y2 = 300
  24. # draw a line from coordinates (x1,y1) to (x2,y2)
  25. dc.DrawLine(x1, y1, x2, y2)
  26.  
  27.  
  28. app = wx.App()
  29. frame = wx.Frame(None, -1, "Draw a line", size=(550, 350))
  30. dp = DrawPanel(frame)
  31. frame.Show(True)
  32. app.MainLoop()
You can get a little more playful, using just about the same frame work:
  1. # the wx.PaintDC surface is wxPython's canvas
  2. # draw a series of lines on this canvas
  3. # DC stands for Device Context
  4.  
  5. import wx
  6.  
  7. class DrawPanel(wx.Panel):
  8. """draw lines on a panel's wx.PaintDC surface/canvas"""
  9. def __init__(self, parent):
  10. wx.Panel.__init__(self, parent, -1)
  11. # bind the panel to the paint event
  12. wx.EVT_PAINT(self, self.onPaint)
  13.  
  14. def onPaint(self, event=None):
  15. # this is the wx drawing surface/canvas
  16. dc = wx.PaintDC(self)
  17. dc.Clear()
  18. # sets pen color and width
  19. dc.SetPen(wx.Pen("red", 1))
  20. # set the starting point coordinates to (0,0)
  21. x1 = y1 = 0
  22. # use a loop to set the endpoint coordinates (x2,y2)
  23. # and draw a series of lines
  24. for y2 in range(100, 400, 20):
  25. x2 = 500
  26. dc.DrawLine(x1, y1, x2, y2)
  27.  
  28.  
  29. app = wx.App()
  30. frame = wx.Frame(None, -1, "Draw lines", size=(550, 420))
  31. dp = DrawPanel(frame)
  32. frame.Show(True)
  33. app.MainLoop()
Let's finish this off with the circle method:
  1. # the wx.PaintDC surface is wxPython's canvas
  2. # draw 2 circles on this canvas
  3. # DC stands for Device Context
  4.  
  5. import wx
  6.  
  7. class DrawPanel(wx.Panel):
  8. """draw circles on a panel's wx.PaintDC surface/canvas"""
  9. def __init__(self, parent):
  10. wx.Panel.__init__(self, parent, -1)
  11. # bind the panel to the paint event
  12. wx.EVT_PAINT(self, self.onPaint)
  13.  
  14. def onPaint(self, event=None):
  15. # this is the wx drawing surface/canvas
  16. dc = wx.PaintDC(self)
  17. dc.Clear()
  18. # sets pen color and width
  19. dc.SetPen(wx.Pen("black", 2))
  20. # sets the fill color (default is background)
  21. dc.SetBrush(wx.Brush('yellow'))
  22. x = 200
  23. y = 200
  24. r = 150
  25. # draw a circle with center coordinates (x,y) and radius r
  26. dc.DrawCircle(x, y, r)
  27. # change the fill color
  28. dc.SetBrush(wx.Brush('red'))
  29. # draw a smaller circle
  30. r = 20
  31. dc.DrawCircle(x, y, r)
  32.  
  33.  
  34. app = wx.App()
  35. frame = wx.Frame(None, -1, "Draw circles", size=(420, 420))
  36. dp = DrawPanel(frame)
  37. frame.Show(True)
  38. app.MainLoop()
Now it's up to you to do some fancy random art.
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,222
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 136
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: Starting wxPython (GUI code)

 
2
  #12
Jun 10th, 2008
Nice stuff there Fuse and Ene!
Here is my contribution, showing you how easy it is to have wxPython display an image from image file:
  1. # show .jpg .png .bmp or .gif image on wx.Panel
  2.  
  3. import wx
  4.  
  5. class ImagePanel(wx.Panel):
  6. """ create the panel and put image on it """
  7. def __init__(self, parent, id):
  8. # create the panel, this will be self
  9. wx.Panel.__init__(self, parent, id)
  10. try:
  11. # pick your image file you have in the working folder
  12. # or use the full file path
  13. image_file = 'strawberry.jpg'
  14. bmp = wx.Bitmap(image_file)
  15. # show the bitmap, image's upper left corner anchors
  16. # at panel coordinates (5, 5), default is center
  17. wx.StaticBitmap(self, -1, bmp, (5, 5))
  18. # show some image information
  19. info = "%s %dx%d" % (image_file, bmp.GetWidth(), bmp.GetHeight())
  20. # the parent is the frame
  21. parent.SetTitle(info)
  22. except IOError:
  23. print "Image file %s not found" % imageFile
  24. raise SystemExit
  25.  
  26.  
  27. # redirect=False sends stdout/stderr to the console window
  28. # redirect=True sends stdout/stderr to a wx popup window (default)
  29. app = wx.App(redirect=False)
  30. # create window/frame, no parent, -1 is the default ID
  31. # also increase the size of the frame for larger images
  32. frame = wx.Frame(None, -1, size = (480, 320))
  33. # create the panel instance
  34. imp = ImagePanel(frame, -1)
  35. # show the frame
  36. frame.Show(True)
  37. # start the GUI event loop
  38. app.MainLoop()
Last edited by bumsfeld; Jun 10th, 2008 at 6:27 pm.
Should you find Irony, you can keep her!
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,222
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 136
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: Starting wxPython (GUI code)

 
1
  #13
Jun 10th, 2008
Well, if you do web-page designing, you will be familiar with the repeating image tile wallpaper. You can do something similar to give your wxPython frame or panel very sexy backgrounds. I leaned on one of vegaseat's snippets to come up with this example:
  1. # wallpaper wxPython panel using one image as repeating tile
  2. # (image is obtained from base 64 encoded png image string)
  3.  
  4. import wx
  5. import cStringIO
  6. import base64
  7.  
  8.  
  9. class MyPanel(wx.Panel):
  10. """
  11. class creates panel for the tile image contained in data_stream
  12. """
  13. def __init__(self, parent, id, frame_w, frame_h, data_stream):
  14. # create the panel
  15. wx.Panel.__init__(self, parent, id)
  16. # frame/panel width and height
  17. self.frame_w = frame_w
  18. self.frame_h = frame_h
  19.  
  20. # convert to bitmap
  21. self.bmp = wx.BitmapFromImage(wx.ImageFromStream(data_stream))
  22. # do the wall papering on the PaintDC canvas...
  23. wx.EVT_PAINT(self, self.on_paint)
  24. # now put some widgets on top of the panel's wallpaper
  25. self.button1 = wx.Button(self, -1, label='Button1', pos=(15, 10))
  26. self.button2 = wx.Button(self, -1, label='Button2', pos=(15, 45))
  27.  
  28.  
  29. def on_paint(self, event=None):
  30. # create the paint canvas
  31. dc = wx.PaintDC(self)
  32. dc.Clear()
  33. # get image width and height
  34. image_w = self.bmp.GetWidth()
  35. image_h = self.bmp.GetHeight()
  36. # use repeating image tiles to wallpaper the canvas
  37. for x in range(0, self.frame_w, image_w):
  38. for y in range(0, self.frame_h, image_h):
  39. dc.DrawBitmap(self.bmp, x, y, True)
  40.  
  41.  
  42. # the base64 encoded png image string
  43. png_b64='''\
  44. iVBORw0KGgoAAAANSUhEUgAAAGQAAABkBAMAAACCzIhnAAAAMFBMVEUEAgQEAoYEAkgEAsYEAicE
  45. AqgEAmkEAucEAhgEApgEAlkEAtYEAjgEArkEAnsEAvxjNQuZAAAFBUlEQVR4nNWXT2gcVRjAv91O
  46. l+kyjd02kSSFYY21CtpUGIKHMGTBFkIpgVAeMpFtNlZi20MEc5jDHrKtRXOQ4LYGHA/rKuhBNB4W
  47. CXQM2QY9GEWEdSgxPLsvhNBoO6Gp6dI/OH5vN7U97XsDXvwOe3o/vj/z5pvfQsDjHoC2UoTm0d0O
  48. Px+4sP8lqCN3EdkrQpRk/GrNDoJHyBdJAQLJM1f54UfI5ykBoabUrqcfR2LdoiQYGmw8RG4hclYC
  49. UWHXQ2QiBVOvSyA4g+Ft5OtfYCEphUCkgdzf8QCuyxEAz9eRy3FLLcoB/bk3OXI/p1jxnBQReyMb
  50. wN8f47wUS7YsPrG/duENU3+XRaKI3IHDD0A5KYtAXwD3YNcmKAVpZCdHIndBGZRG4Bosws4tUPLy
  51. iAar8OMQKE/KIwBVGGmF+I0wSA4u7IGWqjwQ5T97IJILk6UeB0IT/79QDp79VOpgLoersdeseGsn
  52. iGclxcAAmeBvaq02u2yYnjX9vpD4LGjErDteqqQ9kv9QiJAGUatlXaPiEcv5QKKw+Tpju6USTRMn
  53. v1eMQPQaX4azbsmkHnEKKxIIf7+Del003TGoy2TB7RbU7Oy4QQlxOhJy74U2UbN5Em+JJArtUghs
  54. 2Fm316RpyymwVTlkN9aF3VtOXtd9OeTIMk9CrOk8W2Pl5mcvHDl+cHRthLUtG5UKIdY6W2XN+59h
  55. jfD1cyV8KFYizzLdfzRFcsd8fl5PsLhpEJLHVsrvjOWaVzaA5y8ZprOPUs+ZdhgrxkTNKL6+v0Rd
  56. 1zCXPGeQsZlUfKbYHME0jK3PlswKdayOgv4CaGsiBMawtOWSQa02PuKihKlAC07sxBylQ5F8gnVi
  57. qXszzYGFL/nE/jQqZEjFuvCZaKuCj7WawOfNPjFMMgTnEqwVkfY9grpG/MwVVk5TRLCtKiLXRcgU
  58. q6oMzhHyEcAVfo219qoAUf3rMJCCm/Sb0cVX7G95LyIE4hn1VE9jlwXBHYmJYSRPbZ/fdruVpBAp
  59. K6PDjyNig1RBPa/NbyNbuG+EBslDgbe3kVuSBokRnfi3sNhrcgi83EDmkzAgZ5DYUV8dOfwiHChL
  60. Io1ubu9+S9YgY22J+sx+jRIlKZljB1/+wW1QnGhOEokFweZGKIMEZT54EAs2w+ggPIvG+W4ogwQF
  61. kUgogwQ4ehkiW2F6weiCHSidehikHb66CFpnCELNwMxqOIPUkEKDzIQpDCMDkZAExnPhkf80lP7J
  62. EKfXRs8eP4Nyl5IFxsDmX3jPskS7Gmfe1UYIma9B1p0zqIzc1Z2LixpkswbP4gjt5uE2tGHcNWja
  63. cfIXRcjk/oZB2mCbRg/KXUJC7r6vJ8lCCY0o7VgFGbk72iis1Gv2YCtMSiGHsS4XXEqnl5y8nNy1
  64. BDYW5lbQu5w8k1uKfXYWezHpkoVGJPf6PWFnS1Cpe5eus5QMos26Lsw16lrxTwtOq+8tLCyOXsIs
  65. tIeQjnU9M9J0ysfGfJ/roK4fcoGml3iW5MBYM+SnGb0ejCkGmEvTToL5mdhYuWldnOlwHHaIArXw
  66. tugj5bhfbIpEdf3mRNY2zAp4noOeehA0kQ8+pbcl1rlBQsNTUe46BYjm+/q6W5qjMPSDc1LvzEn4
  67. 4BR28+pchcJvmqPz/yjKjeav8mR/Nw55n2l6MATdut4q1q7zdUm/iX9qEIkyVuVyJ7jLjJ0eYd/h
  68. JQZpubvCqnE99Uya/APf6sMx1/lYCAAAAABJRU5ErkJggg==
  69. '''
  70.  
  71. # convert to png image bytes
  72. png_bytes = base64.b64decode(png_b64)
  73. # convert png bytes to data stream
  74. data_stream = cStringIO.StringIO(png_bytes)
  75.  
  76. app = wx.App()
  77. # create window/frame instance, no parent, -1 is default ID
  78. frame_w = 500
  79. frame_h = 400
  80. frame = wx.Frame(None, -1, "tiling the wx.PaintDC canvas",
  81. size=(frame_w, frame_h))
  82. # create panel class instance
  83. mp = MyPanel(frame, -1, frame_w, frame_h, data_stream)
  84. frame.Show(True)
  85. # start the event loop
  86. app.MainLoop()
Should you find Irony, you can keep her!
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 46
Reputation: Fuse is an unknown quantity at this point 
Solved Threads: 3
Fuse's Avatar
Fuse Fuse is offline Offline
Light Poster

Re: Starting wxPython (GUI code)

 
1
  #14
Jun 11th, 2008
paulthom had an idea of adding help text to a texbox when the programme loads. The help text just explains what to type in the box and needs to disappear once the user types anything or clicks in the box. If you're interested in doing similar, or want to know how to use mouse events, go here:

http://www.daniweb.com/forums/thread128788.html

Aside from being example code that shows you how to implement such a feature, it demonstrates how funny things happen if you don't return focus to the calling widget by typing event.Skip() at the end of the mouse event's handler method.

The wxPython documentation on mouse events is a decent reference: http://www.wxpython.org/docs/api/wx....ent-class.html
Last edited by Fuse; Jun 11th, 2008 at 6:01 am.
Mir's Fuselage

Because what's not to like about being killed by a toilet seat from Mir's atmospheric re-entry?
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1,522
Reputation: Lardmeister is an unknown quantity at this point 
Solved Threads: 22
Lardmeister's Avatar
Lardmeister Lardmeister is offline Offline
Posting Virtuoso

Re: Starting wxPython (GUI code)

 
1
  #15
Jun 12th, 2008
To get used to wxPython, I took vegaseat's mortgage calculator snippet written in Csharp and converted it to Python. I was surprised how easy it was:
  1. # a simple mortgage calulator using wxPython
  2. # checked it out with the online mortgage calculator at:
  3. # http://www.mortgage-calc.com/mortgage/simple.php
  4.  
  5. import wx
  6. import math
  7.  
  8. class MyFrame(wx.Frame):
  9. """frame and widgets to handle input and output of mortgage calc"""
  10. def __init__(self, parent, id, title):
  11. wx.Frame.__init__(self, parent, id, title)
  12. # add panel, labels, text and sizer widgets
  13. panel = wx.Panel(self, -1)
  14. panel.SetBackgroundColour('green')
  15. label1 = wx.StaticText(panel, -1, "Enter total loan amount:")
  16. label2 = wx.StaticText(panel, -1, "Enter annual interest (%):")
  17. label3 = wx.StaticText(panel, -1, "Enter years to pay:")
  18. self.loan = wx.TextCtrl(panel, -1, "100000")
  19. self.interest = wx.TextCtrl(panel, -1, "6.5")
  20. self.years = wx.TextCtrl(panel, -1, "30")
  21. self.calc_btn = wx.Button(panel, -1, ' Perform Mortgage Calculation ')
  22. self.calc_btn.SetBackgroundColour('light blue')
  23. self.calc_btn.Bind(wx.EVT_BUTTON, self.onCalc)
  24. info = "Modify the above data to your needs!"
  25. self.result = wx.TextCtrl(panel, -1, info, size=(290, 100),
  26. style=wx.TE_MULTILINE)
  27.  
  28. # use gridbagsizer for layout of widgets
  29. sizer = wx.GridBagSizer(vgap=5, hgap=10)
  30. sizer.Add(label1, pos=(0, 0))
  31. sizer.Add(self.loan, pos=(0, 1)) # row 0, column 1
  32. sizer.Add(label2, pos=(1, 0))
  33. sizer.Add(self.interest, pos=(1, 1))
  34. sizer.Add(label3, pos=(2, 0))
  35. sizer.Add(self.years, pos=(2, 1))
  36. sizer.Add(self.calc_btn, pos=(3, 0), span=(1, 2))
  37. # span=(1, 2) --> allow to span over 2 columns
  38. sizer.Add(self.result, pos=(4, 0), span=(1, 2))
  39.  
  40. # use boxsizer to add border around sizer
  41. border = wx.BoxSizer()
  42. border.Add(sizer, 0, wx.ALL, 20)
  43. panel.SetSizerAndFit(border)
  44. self.Fit()
  45.  
  46. def onCalc(self, event):
  47. """do the mortgage calcuations"""
  48. # get the values from the input widgets
  49. principal = float(self.loan.GetValue())
  50. interest = float(self.interest.GetValue())
  51. years = float(self.years.GetValue())
  52. # calculate
  53. interestRate = interest/(100 * 12)
  54. paymentNum = years * 12
  55. paymentVal = principal * \
  56. (interestRate/(1-math.pow((1+interestRate), (-paymentNum))))
  57. # show the result
  58. resultStr1 = "Your monthly payment will be $%.2f for\n" % paymentVal
  59. resultStr2 = "a %.1f year $%.2f loan at %.2f%s interest" % \
  60. (years, principal, interest, '%')
  61. self.result.SetValue(resultStr1 + resultStr2)
  62.  
  63.  
  64. app = wx.App()
  65. frame = MyFrame(None, -1, "Mortgage Calculator")
  66. frame.Show()
  67. app.MainLoop()
I upped my sanitary measures, up yours!
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 666
Reputation: ZZucker is on a distinguished road 
Solved Threads: 38
ZZucker's Avatar
ZZucker ZZucker is offline Offline
Practically a Master Poster

Re: Starting wxPython (GUI code)

 
1
  #16
Jun 13th, 2008
I took Lardmeister's code and made a generic wxPython templet that you can then flesh out for cases when you need the user to enter data, press a button to process the data, and then show the result in an output area:
  1. # basic wx.Frame with panel (needed for sizers), label,
  2. # edit, button, display, sizer, and border sizer
  3.  
  4. import wx
  5.  
  6. class MyFrame(wx.Frame):
  7. """
  8. frame and panel
  9. panel is neded for any sizer widgets
  10. """
  11. def __init__(self, parent, id, title):
  12. # this will be self
  13. wx.Frame.__init__(self, parent, id, title)
  14. # add panel
  15. panel = wx.Panel(self, -1)
  16. panel.SetBackgroundColour('green')
  17.  
  18. # now add the needed widgets
  19. self.label1 = wx.StaticText(panel, -1, 'Enter ... :')
  20. self.entry1 = wx.TextCtrl(panel, -1, '...')
  21. self.button1 = wx.Button(panel, -1, 'Do ... ')
  22. self.button1.SetBackgroundColour('yellow')
  23. self.button1.Bind(wx.EVT_BUTTON, self.onCmd)
  24. info = "Optional instructive message!"
  25. self.display = wx.TextCtrl(panel, -1, info, size=(250, 100),
  26. style=wx.TE_MULTILINE)
  27.  
  28. # use gridbagsizer for layout of widgets
  29. # set optional vertical and horizontal gaps
  30. sizer = wx.GridBagSizer(vgap=5, hgap=10)
  31. sizer.Add(self.label1, pos=(0, 0)) # pos(row,column)
  32. sizer.Add(self.entry1, pos=(0, 1)) # row 0, column 1
  33.  
  34. # span=(1, 2) --> allow to span over 2 columns
  35. sizer.Add(self.button1, pos=(1, 0), span=(1, 2))
  36. sizer.Add(self.display, pos=(2, 0), span=(1, 2))
  37.  
  38. # use boxsizer to add border around sizer
  39. border = wx.BoxSizer()
  40. border.Add(sizer, 0, wx.ALL, 20)
  41. panel.SetSizerAndFit(border)
  42. self.Fit()
  43.  
  44. def onCmd(self, event):
  45. """process data and show result"""
  46. # get the data from the input widget
  47. string1 = self.entry1.GetValue()
  48. # do the processing ...
  49. proc_data = string1 * 3
  50. # show the result ...
  51. self.display.SetValue(proc_data)
  52.  
  53.  
  54. app = wx.App(redirect=False)
  55. frame = MyFrame(None, -1, "Title ...")
  56. frame.Show()
  57. app.MainLoop()
Note how we take care of an "Optional instructive message" without much fanfare.
Last edited by ZZucker; Jun 13th, 2008 at 2:42 pm.
Never argue with idiots, they'll just bring you down to their level and beat you with their experience.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 666
Reputation: ZZucker is on a distinguished road 
Solved Threads: 38
ZZucker's Avatar
ZZucker ZZucker is offline Offline
Practically a Master Poster

Re: Starting wxPython (GUI code)

 
2
  #17
Jun 13th, 2008
I took the above wxPython templet and added a background or splash image. So now you have a templet that shows you how to create a frame, a panel, a label, an entry (input), a button, sizers, a multiline display and show an image. Could be the backbone of many wxPython GUI applications:
  1. # basic wx.Frame with splash image panel (panel needed for sizers)
  2. # label, edit, button, display, sizer, and border sizer
  3.  
  4. import wx
  5.  
  6. class MyFrame(wx.Frame):
  7. """
  8. frame and panel
  9. panel is neded for any sizer widgets
  10. """
  11. def __init__(self, parent, id, title):
  12. # this will be self
  13. wx.Frame.__init__(self, parent, id, title)
  14. # add panel
  15. panel = wx.Panel(self, -1)
  16.  
  17. # pick a splash image file you have in the working folder
  18. image_file = 'HIVirus1.jpg'
  19. bmp = wx.Bitmap(image_file)
  20. # allow for alternative if splash image file not found
  21. if bmp:
  22. splash = wx.StaticBitmap(panel, -1, bmp)
  23. else:
  24. panel.SetBackgroundColour('green')
  25. splash = panel
  26.  
  27. # now add the needed widgets
  28. self.label1 = wx.StaticText(splash, -1, 'Enter ... :')
  29. self.entry1 = wx.TextCtrl(splash, -1, '...')
  30. self.button1 = wx.Button(splash, -1, 'Do ... ')
  31. self.button1.SetBackgroundColour('yellow')
  32. self.button1.Bind(wx.EVT_BUTTON, self.onCmd)
  33. info = "Optional instructive message!"
  34. self.display = wx.TextCtrl(splash, -1, info, size=(250, 100),
  35. style=wx.TE_MULTILINE)
  36.  
  37. # use gridbagsizer for layout of widgets
  38. # set optional vertical and horizontal gaps
  39. sizer = wx.GridBagSizer(vgap=5, hgap=10)
  40. sizer.Add(self.label1, pos=(0, 0)) # pos(row,column)
  41. sizer.Add(self.entry1, pos=(0, 1)) # row 0, column 1
  42.  
  43. # span=(1, 2) --> allow to span over 2 columns
  44. sizer.Add(self.button1, pos=(1, 0), span=(1, 2))
  45. sizer.Add(self.display, pos=(2, 0), span=(1, 2))
  46.  
  47. # use boxsizer to add border around sizer
  48. border = wx.BoxSizer()
  49. border.Add(sizer, 0, wx.ALL, 30)
  50. panel.SetSizerAndFit(border)
  51. self.Fit()
  52.  
  53. def onCmd(self, event):
  54. """process data and show result"""
  55. # get the data from the input widget
  56. string1 = self.entry1.GetValue()
  57. # do the processing ...
  58. proc_data = string1 * 3
  59. # show the result ...
  60. self.display.SetValue(proc_data)
  61.  
  62.  
  63. app = wx.App(redirect=False) # stdout/stderr to console
  64. frame = MyFrame(None, -1, "Title ...")
  65. # optional info, size seems to adjust to sizers demand
  66. print frame.GetSize() # (336, 250)
  67. frame.Show()
  68. app.MainLoop()
Last edited by ZZucker; Jun 13th, 2008 at 7:11 pm.
Attached Thumbnails
HIVirus1.jpg  
Never argue with idiots, they'll just bring you down to their level and beat you with their experience.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,275
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 175
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Starting wxPython (GUI code)

 
1
  #18
Jun 29th, 2008
The wxPython GUI toolkit has some interesting widgets, one of them is a complete analog clock widget. Here is an example:
  1. # looking at the wxPython analog clock widget
  2.  
  3. import wx
  4. from wx.lib import analogclock as ac
  5.  
  6. class MyFrame(wx.Frame):
  7. def __init__(self, parent, id, title):
  8. wx.Frame.__init__(self, parent, id, title)
  9.  
  10. clock = ac.AnalogClockWindow(self)
  11. clock.SetBackgroundColour('gray')
  12. clock.SetHandColours('black')
  13. clock.SetTickColours('WHITE')
  14. # set hour and minute ticks
  15. clock.SetTickSizes(h=15, m=5)
  16. # set hour style
  17. clock.SetTickStyles(ac.TICKS_ROMAN)
  18. self.SetSize((400,350))
  19.  
  20.  
  21. app = wx.App()
  22. frame = MyFrame(None, wx.ID_ANY, "analogclock")
  23. frame.Show()
  24. app.MainLoop()
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,275
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 175
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Starting wxPython (GUI code)

 
1
  #19
Jun 29th, 2008
This code sample shows you how to add an about message box to your wxPython program:
  1. # wxPython Frame with menu, statusbar, and about dialog
  2.  
  3. import wx
  4.  
  5. class MyFrame(wx.Frame):
  6. """
  7. create a frame, with menu, statusbar and about dialog
  8. inherits wx.Frame
  9. """
  10. def __init__(self):
  11. # create a frame/window, no parent, default to wxID_ANY
  12. wx.Frame.__init__(self, None, wx.ID_ANY, 'About (click File)',
  13. pos=(300, 150), size=(300, 350))
  14.  
  15. # create a status bar at the bottom
  16. self.CreateStatusBar()
  17. self.SetStatusText("This is the statusbar")
  18.  
  19. menu = wx.Menu()
  20. # the optional & allows you to use alt/a
  21. # the last string argument shows in the status bar on mouse_over
  22. menu_about = menu.Append(wx.ID_ANY, "&About", "About message")
  23. menu.AppendSeparator()
  24. # the optional & allows you to use alt/x
  25. menu_exit = menu.Append(wx.ID_ANY, "E&xit", "Quit the program")
  26.  
  27. # create a menu bar at the top
  28. menuBar = wx.MenuBar()
  29. # the & allows you to use alt/f
  30. menuBar.Append(menu, "&File")
  31. self.SetMenuBar(menuBar)
  32.  
  33. # bind the menu events to an action/function/method
  34. self.Bind(wx.EVT_MENU, self.onMenuAbout, menu_about)
  35. self.Bind(wx.EVT_MENU, self.onMenuExit, menu_exit)
  36.  
  37. def onMenuAbout(self, event):
  38. dlg = wx.MessageDialog(self,
  39. "a simple application using wxFrame, wxMenu\n"
  40. "a statusbar, and this about message. snee",
  41. "About", wx.OK | wx.ICON_INFORMATION)
  42. dlg.ShowModal()
  43. dlg.Destroy()
  44.  
  45. def onMenuExit(self, event):
  46. # via wx.EVT_CLOSE event
  47. self.Close(True)
  48.  
  49. app = wx.App(0)
  50. # create class instance
  51. window = MyFrame()
  52. window.Show(True)
  53. # start the event loop
  54. app.MainLoop()
Last edited by sneekula; Jun 29th, 2008 at 11:56 am.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,514
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 168
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Starting wxPython (GUI code)

 
1
  #20
Jun 30th, 2008
The wxPython GUI toolkit has a number of ways to specify colors, or should I say colours. Here is a simple example:
  1. # show different ways to specify colours with wxPython
  2.  
  3. import wx
  4.  
  5. class ColourTest(wx.Dialog):
  6. def __init__(self, parent, id, title):
  7. """use a dialog box as a simple window/frame"""
  8. wx.Dialog.__init__(self, parent, id, title, size=(300, 300))
  9.  
  10. self.pnl1 = wx.Panel(self, -1)
  11. self.pnl2 = wx.Panel(self, -1)
  12. self.pnl3 = wx.Panel(self, -1)
  13. self.pnl4 = wx.Panel(self, -1)
  14. self.pnl5 = wx.Panel(self, -1)
  15. self.pnl6 = wx.Panel(self, -1)
  16. self.pnl7 = wx.Panel(self, -1)
  17. self.pnl8 = wx.Panel(self, -1)
  18.  
  19. # use a wx.GridSizer(rows, cols, vgap, hgap) for layout
  20. gs = wx.GridSizer(4,2,3,3)
  21. # for this dialog window wx.EXPAND is not needed
  22. gs.AddMany([ (self.pnl1, 0 ,wx.EXPAND),
  23. (self.pnl2, 0, wx.EXPAND),
  24. (self.pnl3, 0, wx.EXPAND),
  25. (self.pnl4, 0, wx.EXPAND),
  26. (self.pnl5, 0, wx.EXPAND),
  27. (self.pnl6, 0, wx.EXPAND),
  28. (self.pnl7, 0, wx.EXPAND),
  29. (self.pnl8, 0, wx.EXPAND) ])
  30.  
  31. self.SetSizer(gs)
  32. self.SetColors()
  33. self.Centre()
  34. self.ShowModal()
  35. self.Destroy()
  36.  
  37. def SetColors(self):
  38. # create a number of colorful panels
  39. # using different ways to specify colours
  40. self.pnl1.SetBackgroundColour(wx.BLACK)
  41. # wx.Colour() uses a (r, g, b) tuple
  42. self.pnl2.SetBackgroundColour(wx.Colour(139,105,20))
  43. self.pnl3.SetBackgroundColour(wx.RED)
  44. # specify as #RRGGBB hex string
  45. self.pnl4.SetBackgroundColour('#0000FF')
  46. self.pnl5.SetBackgroundColour('dark green')
  47. self.pnl6.SetBackgroundColour('midnight blue')
  48. self.pnl7.SetBackgroundColour(wx.LIGHT_GREY)
  49. self.pnl8.SetBackgroundColour('plum')
  50.  
  51.  
  52. app = wx.App()
  53. ColourTest(None, wx.ID_ANY, 'wxPython colours')
  54. app.MainLoop()
Last edited by Ene Uran; Jun 30th, 2008 at 11:00 am.
drink her pretty
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC