Starting wxPython (GUI code)

Reply

Join Date: Oct 2004
Posts: 3,946
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 913
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Starting wxPython (GUI code)

 
0
  #31
Jul 15th, 2008
Sizers look a little complex at first, but they can make your component layouts a lot simpler. The wx.GridSizer() is particularly well suited for a bunch of similar widgets generated with a for loop. In this case I used the gridsizer for the buttons of a simple calculator. The buttons simply keep wrapping from left to right, down a notch then left to right again ...
  1. # create a calulator button layout with wx.GridSizer()
  2. # then add a few things to form a tiny wxPython calculator
  3.  
  4. import wx
  5.  
  6. class MyFrame(wx.Frame):
  7. """make a frame, inherits wx.Frame"""
  8. def __init__(self):
  9. # create a frame/window, no parent
  10. wx.Frame.__init__(self, None, wx.ID_ANY, 'wx_Calc',
  11. pos=(300, 150), size=(185, 160))
  12. self.SetBackgroundColour('green')
  13. # main sizer
  14. vsizer = wx.BoxSizer(wx.VERTICAL)
  15.  
  16. self.edit = wx.TextCtrl(self, -1, value="", size=(165, 20))
  17.  
  18. # follows layout of calculator keys
  19. self.btn_list = [
  20. '7', '8', '9', '/', 'c',
  21. '4', '5', '6', '*', 'bs',
  22. '1', '2', '3', '-', '**',
  23. '0', '.', '=', '+', 'neg'
  24. ]
  25.  
  26. # wx.GridSizer(rows, cols, vgap, hgap)
  27. gsizer = wx.GridSizer(4, 5, 2, 2)
  28.  
  29. self.btn = range(len(self.btn_list))
  30. for ix, b_label in enumerate(self.btn_list):
  31. # set up a consecutive unique id for each button
  32. id = 1000 + ix
  33. self.btn[ix] = wx.Button(self, id, label=b_label, size=(20, 20))
  34. # the gridsizer fills left to right one row at a time
  35. gsizer.Add(self.btn[ix], 0, wx.ALL|wx.EXPAND, border=2)
  36. self.btn[ix].Bind(wx.EVT_BUTTON, self.btnClick)
  37.  
  38. # now add the whole thing to the main sizer and set it
  39. vsizer.Add(self.edit, 0, wx.EXPAND)
  40. vsizer.Add(gsizer, 0, wx.EXPAND)
  41. self.SetSizer(vsizer)
  42.  
  43. def btnClick(self, event):
  44. # get the label of the button clicked
  45. label = self.btn_list[event.GetId() - 1000]
  46. e_list = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  47. '+', '-', '*', '/', '**', '.']
  48. if label in e_list:
  49. self.edit.SetValue(self.edit.GetValue() + label)
  50. elif label == 'neg':
  51. # negate, note eval() takes care of double negate
  52. self.edit.SetValue('-' + self.edit.GetValue())
  53. elif label == 'c':
  54. # clear
  55. self.edit.SetValue('')
  56. elif label == 'bs':
  57. # backspace
  58. self.edit.SetValue(self.edit.GetValue()[:-1])
  59. elif label == '=':
  60. str1 = self.edit.GetValue()
  61. # prevent folks from being nasty with eval()
  62. if not str1 or str1[0] not in '0123456789-+.':
  63. self.edit.SetValue('unrecognized operation')
  64. return
  65. while str1[0] == '0':
  66. # avoid leading zero (octal) error with eval()
  67. str1 = str1[1:]
  68. if '/' in str1 and '.' not in str1:
  69. # turn into floating point division
  70. str1 = str1 + '.0'
  71. try:
  72. self.edit.SetValue(str(eval(str1)))
  73. except ZeroDivisionError:
  74. self.edit.SetValue('division by zero error')
  75. else:
  76. self.edit.SetValue('unrecognized operation')
  77.  
  78.  
  79. app = wx.App(0)
  80. # create MyFrame instance and show the frame
  81. MyFrame().Show()
  82. app.MainLoop()
May 'the Google' be with you!
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
  #32
Jul 16th, 2008
I was playing around with wxPython's slider widget and found a nice application for it:
  1. # use slider inputs to calculate cost of petrol in the USA and Europe
  2.  
  3. import wx
  4.  
  5. class MyFrame(wx.Frame):
  6. def __init__(self, parent, mytitle, mysize):
  7. wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle, size=mysize)
  8. self.SetBackgroundColour("yellow")
  9.  
  10. # create input widgets
  11. # label for slider1
  12. label_s1 = wx.StaticText(self, wx.ID_ANY, "US cents per US Gallon:")
  13. # can only use integer values!!!
  14. # initial value = 450, min value = 300, max value = 600
  15. self.slider1 = wx.Slider(self, wx.ID_ANY, 450, 300, 600, size=(320, 40),
  16. style=wx.SL_HORIZONTAL|wx.SL_LABELS)
  17. # label for slider2
  18. label_s2 = wx.StaticText(self, wx.ID_ANY, "Euro cents per Liter:")
  19. # initial value = 150, min value = 100, max value = 200
  20. self.slider2 = wx.Slider(self, wx.ID_ANY, 150, 100, 200, size=(320, 40),
  21. style=wx.SL_HORIZONTAL|wx.SL_LABELS)
  22. # label for slider3
  23. label_s3 = wx.StaticText(self, wx.ID_ANY, "US cents per Euro:")
  24. # initial value = 160, min value = 100, max value = 200
  25. self.slider3 = wx.Slider(self, wx.ID_ANY, 160, 100, 200, size=(320, 40),
  26. style=wx.SL_HORIZONTAL|wx.SL_LABELS)
  27.  
  28. # bind all mouse slider marker drags to the same action
  29. self.Bind(wx.EVT_SLIDER, self.onAction)
  30.  
  31. # create an output widget
  32. self.label = wx.StaticText(self, wx.ID_ANY, "")
  33.  
  34. # use a vertical boxsizer for the widget placement
  35. sizer_v = wx.BoxSizer(wx.VERTICAL)
  36. sizer_v.Add(label_s1, 0, flag=wx.LEFT|wx.RIGHT|wx.EXPAND, border=10)
  37. sizer_v.Add(self.slider1, 0, flag=wx.ALL|wx.EXPAND, border=5)
  38. sizer_v.Add(label_s2, 0, flag=wx.LEFT|wx.RIGHT|wx.EXPAND, border=10)
  39. sizer_v.Add(self.slider2, 0, flag=wx.ALL|wx.EXPAND, border=5)
  40. sizer_v.Add(label_s3, 0, flag=wx.LEFT|wx.RIGHT|wx.EXPAND, border=10)
  41. sizer_v.Add(self.slider3, 0, flag=wx.ALL|wx.EXPAND, border=5)
  42. sizer_v.Add(self.label, 0, flag=wx.ALL|wx.EXPAND, border=10)
  43. self.SetSizer(sizer_v)
  44.  
  45. # show opening result
  46. self.onAction(None)
  47.  
  48. def onAction(self, event):
  49. """ some action code"""
  50. s = "The result ... \n\n"
  51. # gives integer cents values, convert to $ and Euro
  52. us_price = self.slider1.GetValue()/100.0
  53. euro_price = self.slider2.GetValue()/100.0
  54. euro_cost = self.slider3.GetValue()/100.0
  55. # 1 US gal = 3.785 liters
  56. s1 = "In the USA $%.2f/gal = $%.2f/liter = %.2f Euro/liter\n" % \
  57. (us_price, us_price/3.785, us_price/(3.785*euro_cost))
  58. s2 = "In Europe $%.2f/gal = $%.2f/liter = %.2f Euro/liter" % \
  59. (euro_price*euro_cost*3.785, euro_price*euro_cost, euro_price)
  60. self.label.SetLabel(s + s1 + s2)
  61.  
  62.  
  63. app = wx.App()
  64. # create the MyFrame instance and then show the frame
  65. MyFrame(None, 'The petrol sliders', (350, 300)).Show()
  66. 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
  #33
Jul 16th, 2008
A nice looking informative about-box is easy to achieve with the wx.AboutBox() widget. The example also touches on menu construction and wxPython's wordwrap feature:
  1. # testing the fancy wx.AboutBox() widget
  2.  
  3. import wx
  4. from wx.lib.wordwrap import wordwrap
  5.  
  6. class MyFrame(wx.Frame):
  7. """
  8. create a frame, with a menu, statusbar and 2 about dialogs
  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, "wx.AboutBox test",
  13. pos=(300, 150), size=(300, 350))
  14. self.SetBackgroundColour("brown")
  15.  
  16. # create a status bar at the bottom
  17. self.CreateStatusBar()
  18. self.SetStatusText("Click on File")
  19.  
  20. menu = wx.Menu()
  21. # the optional & allows you to use alt/a
  22. # the last string argument shows in the status bar on mouse_over
  23. menu_about = menu.Append(wx.ID_ANY, "&About", "Ho-hum about box")
  24. menu_about2 = menu.Append(wx.ID_ANY, "About&2", "Fancy about box")
  25. menu.AppendSeparator()
  26. # the optional & allows you to use alt/x
  27. menu_exit = menu.Append(wx.ID_ANY, "E&xit", "Quit the program")
  28.  
  29. # create a menu bar at the top
  30. menuBar = wx.MenuBar()
  31. # the & allows you to use alt/f
  32. menuBar.Append(menu, "&File")
  33. self.SetMenuBar(menuBar)
  34.  
  35. # bind the menu events to an action/function/method
  36. self.Bind(wx.EVT_MENU, self.onMenuAbout, menu_about)
  37. self.Bind(wx.EVT_MENU, self.onMenuAbout2, menu_about2)
  38. self.Bind(wx.EVT_MENU, self.onMenuExit, menu_exit)
  39.  
  40. def onMenuAbout(self, event):
  41. """a somewhat ho-hum about box"""
  42. dlg = wx.MessageDialog(self,
  43. "a simple application using wxFrame, wxMenu\n"
  44. "a statusbar, and this about message.",
  45. "About", wx.OK | wx.ICON_INFORMATION)
  46. dlg.ShowModal()
  47. dlg.Destroy()
  48.  
  49. def onMenuAbout2(self, event):
  50. """use the much fancier wx.AboutBox()"""
  51. # first fill the info object
  52. info = wx.AboutDialogInfo()
  53. info.Name = "Bratwurst7"
  54. info.Version = "v.1.7.4"
  55. info.Copyright = "(C) copyfight 2008"
  56. info.Description = wordwrap(
  57. "The Bratwurst7 program is a software program that "
  58. "makes you desire a freshly grilled bratwurst and "
  59. "a good German beer right now! Teaching programmers "
  60. "everywhere to be aware of those hidden immediate "
  61. "inner desires!",
  62. 300, wx.ClientDC(self))
  63. info.WebSite = ("http://en.wikipedia.org/wiki/Bratwurst",
  64. "Bratwurst7 home")
  65. info.Developers = ["Carl Arm", "Carol Bein", "Candy Kisser"]
  66. info.License = "Wish upon a star!"
  67. # now call wx.AboutBox with this info object
  68. wx.AboutBox(info)
  69.  
  70. def onMenuExit(self, event):
  71. self.Close(True)
  72.  
  73.  
  74. app = wx.App()
  75. # create the MyFrame class instance, then show the frame
  76. MyFrame().Show()
  77. app.MainLoop()
No one died when Clinton lied.
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
  #34
Jul 17th, 2008
Some light wxPython stuff. The first example shows you how to bring in your own icon in the title of a frame:
  1. # set the icon of a wx.Frame()
  2.  
  3. import wx
  4.  
  5. app = wx.App(0)
  6.  
  7. frame = wx.Frame(None, wx.ID_ANY, title='Set A New Icon')
  8. # pick an icon image file you have ...
  9. frame.SetIcon(wx.Icon('py.ico', wx.BITMAP_TYPE_ICO))
  10. frame.Center()
  11. frame.Show()
  12. app.MainLoop()
The second example uses the wx.EVT_SIZE event to respond to changes in the size of the frame:
  1. # use the wx.EVT_SIZE event to show the frame size
  2. # the title itself may take up 34 pixels of the height
  3.  
  4. import wx
  5.  
  6. class MyFrame(wx.Frame):
  7. def __init__(self, parent):
  8. # use default size and position
  9. wx.Frame.__init__(self, parent, wx.ID_ANY)
  10. self.SetBackgroundColour("yellow")
  11. wx.StaticText(self, wx.ID_ANY, "change the size of the frame", (5,5))
  12.  
  13. # respond to changes in the size of the frame
  14. self.Bind(wx.EVT_SIZE, self.onSize)
  15.  
  16. def onSize(self, event):
  17. """display the current frame size in the title"""
  18. self.SetTitle(str(event.GetSize())) # eg. (400, 489)
  19.  
  20.  
  21. app = wx.App(0)
  22. # create a MyFrame instance and show the frame
  23. MyFrame(None).Show()
  24. app.MainLoop()
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)

 
1
  #35
Jul 18th, 2008
Some heavier wxPython stuff. I was playing around with the wx.ListCtrl() widget, and it took me quite a while to find a way to get a hold of the selected row. Here is a short example of the solution:
  1. # exploring wxPython's
  2. # wx.ListCtrl(parent, id, pos, size, style)
  3. # a fancier list box with a lot of mix-in options
  4. # some of the styles =
  5. # wxLC_REPORT report mode
  6. # wxLC_HRULES draws horizontal rules between rows in report mode
  7. # wxLC_VRULES draws vertical rules between columns in report mode.
  8.  
  9. import wx
  10.  
  11. class MyFrame(wx.Frame):
  12. def __init__(self, parent, data):
  13. # use default size and position
  14. wx.Frame.__init__(self, parent, wx.ID_ANY,
  15. 'Test the wx.ListCtrl()',
  16. size=(400, 220))
  17. self.SetBackgroundColour("yellow")
  18. # make data available to the instance
  19. self.data = data
  20.  
  21. # create the list control
  22. self.lc = wx.ListCtrl(self, wx.ID_ANY, size=(-1, 120),
  23. style=wx.LC_REPORT|wx.SUNKEN_BORDER|wx.LC_HRULES)
  24. # select an item (left mouse click on it) and bind to an action
  25. self.lc.Bind(wx.EVT_LIST_ITEM_SELECTED,self.onAction)
  26.  
  27. self.loadList()
  28.  
  29. # create an output widget
  30. self.label = wx.StaticText(self, wx.ID_ANY, "Select a name")
  31.  
  32. # use a vertical boxsizer for the widget placement
  33. sizer_v = wx.BoxSizer(wx.VERTICAL)
  34. sizer_v.Add(self.lc, 1, flag=wx.ALL|wx.EXPAND, border=10)
  35. sizer_v.Add(self.label, 0, flag=wx.ALL|wx.EXPAND, border=10)
  36. self.SetSizer(sizer_v)
  37.  
  38. def loadList(self):
  39. # first the columns with header titles
  40. self.lc.InsertColumn(0,"Name")
  41. self.lc.SetColumnWidth(0, 200)
  42. self.lc.InsertColumn(1,"Age",wx.LIST_FORMAT_RIGHT)
  43. self.lc.InsertColumn(2,"Weight",wx.LIST_FORMAT_RIGHT)
  44.  
  45. # now each data row
  46. for key, val in self.data.items():
  47. # set max_rows, change if need be
  48. max_rows = 1000
  49. # also sets/updates row index starting at 0
  50. index = self.lc.InsertStringItem(max_rows, val[0])
  51. self.lc.SetStringItem(index, 1, val[1])
  52. self.lc.SetStringItem(index, 2, val[2])
  53. # needed by GetItemData()
  54. self.lc.SetItemData(index, key)
  55.  
  56. def onAction(self, event):
  57. """ some action code"""
  58. # -1 --> get the first item that matches the specified flags
  59. # wx.LIST_NEXT_ALL search for subsequent item by index
  60. # wx.LIST_STATE_SELECTED get the selected item
  61. ix_selected = self.lc.GetNextItem(item=-1,
  62. geometry=wx.LIST_NEXT_ALL,
  63. state=wx.LIST_STATE_SELECTED)
  64. # get the value of the key in dictionary self.data
  65. data_value = self.data[self.lc.GetItemData(ix_selected)]
  66. # pick the name (first item in the value tuple)
  67. name = ' --> ' + data_value[0]
  68. self.label.SetLabel(str(data_value) + name)
  69.  
  70.  
  71. # data to load the listctrl in the form of a dictionary
  72. # the header is ('Name', 'Age', 'Weight')
  73. data = {
  74. 1 : ('Heidi Kalumpa', '36', '127'),
  75. 2 : ('Frank Maruco', '27', '234'),
  76. 3 : ('Larry Pestraus', '19', '315'),
  77. 4 : ('Serge Romanowski', '59', '147'),
  78. 5 : ('Carolus Arm', '94', '102'),
  79. 6 : ('Michel Sargnagel', '21', '175')
  80. }
  81.  
  82. app = wx.App(0)
  83. # create a MyFrame instance and then show the frame
  84. MyFrame(None, data).Show()
  85. app.MainLoop()
Last edited by ZZucker; Jul 18th, 2008 at 1:38 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)

 
1
  #36
Jul 18th, 2008
This might be the simplest way to display an image from a file using the wxPython GUI toolkit:
  1. # simplest way to show an image from a file with wxPython
  2.  
  3. import wx
  4.  
  5. app = wx.App(0)
  6. frame = wx.Frame(None, -1, "Show an image file")
  7.  
  8. # pick an image file you have in the working folder
  9. # (can be a .jpg, .png, ,gif, .bmp image file)
  10. image_file = 'clouds.jpg'
  11. # create an internal image
  12. image = wx.Bitmap(image_file)
  13. # show the image as static bitmap
  14. wx.StaticBitmap(frame, -1, image)
  15.  
  16. frame.Show()
  17. app.MainLoop()
What the heck, lets make it even simpler:
  1. import wx
  2.  
  3. app = wx.App(0)
  4. frame = wx.Frame(None, -1, "Show an image file")
  5. wx.StaticBitmap(frame, -1, wx.Bitmap('clouds.jpg'))
  6. frame.Show()
  7. app.MainLoop()
Last edited by ZZucker; Jul 18th, 2008 at 2:11 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: Oct 2004
Posts: 3,946
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 913
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Starting wxPython (GUI code)

 
0
  #37
Jul 20th, 2008
In a previous example we have learned how to create a wxPython canvas and draw shapes on it. If you want to save your artistic creation to one of the common image files, you have to create the canvas on top of a blank bitmap. Here is an example ...
  1. # create a canvas on top of a blank bitmap
  2. # this allows to save any canvas drawings
  3. # created to a standard image file
  4.  
  5. import wx
  6.  
  7. class MyFrame(wx.Frame):
  8. def __init__(self, parent=None, id=-1, title=None):
  9. wx.Frame.__init__(self, parent, id, title)
  10. self.statbmp = wx.StaticBitmap(self)
  11. self.draw_image()
  12. self.save_image()
  13.  
  14. def draw_image(self):
  15. # select the width and height of the blank bitmap
  16. w, h = 340, 340
  17. # create the blank bitmap as a draw background
  18. draw_bmp = wx.EmptyBitmap(w, h)
  19. # create the canvas on top of the draw_bmp
  20. canvas_dc = wx.MemoryDC(draw_bmp)
  21. # fill the canvas white
  22. canvas_dc.SetBrush(wx.Brush('white'))
  23. canvas_dc.Clear()
  24.  
  25. # draw a bunch of circles ...
  26. # pen colour
  27. canvas_dc.SetPen(wx.Pen('red', 1))
  28. # fill colour
  29. canvas_dc.SetBrush(wx.Brush('yellow'))
  30. for x in range(10, 180, 10):
  31. y = x
  32. r = x
  33. canvas_dc.DrawCircle(x, y, r)
  34.  
  35. # now put the canvas drawing into a bitmap to display it
  36. # remember the canvas is on top of the draw_bmp
  37. self.statbmp.SetBitmap(draw_bmp)
  38.  
  39. def save_image(self):
  40. """save the drawing"""
  41. finished_image = self.statbmp.GetBitmap()
  42. #finished_image.SaveFile("mydrawing.png", wx.BITMAP_TYPE_PNG)
  43. finished_image.SaveFile("mydrawing.jpg", wx.BITMAP_TYPE_JPEG)
  44.  
  45.  
  46. app = wx.App(0)
  47. MyFrame(title='draw and save').Show()
  48. app.MainLoop()
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,946
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 913
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Starting wxPython (GUI code)

 
0
  #38
Jul 20th, 2008
Just a modification of the above scheme. This time we don't use a blank image, but an image we have and, let's say, write some fancy text on the image and then save it ...
  1. # create a canvas on top of an image
  2. # then draw some text on the image
  3. # and save the finishd drawing
  4.  
  5. import wx
  6.  
  7. class MyFrame(wx.Frame):
  8. def __init__(self, parent=None, id=-1, title=None):
  9. wx.Frame.__init__(self, parent, id, title)
  10. self.statbmp = wx.StaticBitmap(self)
  11. self.draw_image()
  12. self.save_image()
  13.  
  14. def draw_image(self):
  15. # make sure you have the image in the working
  16. # directory or give the full path, load the image
  17. image = wx.Bitmap("roses.jpg")
  18. canvas_dc = wx.MemoryDC(image)
  19. # the image is now the background for the canvas
  20. canvas_dc.DrawBitmap(image, 0, 0)
  21.  
  22. face = u'Comic Sans MS'
  23. font = wx.Font(22, wx.SWISS, wx.NORMAL, wx.NORMAL, False, face)
  24. canvas_dc.SetFont(font)
  25. canvas_dc.SetTextForeground('red')
  26. canvas_dc.DrawText("Sweet Roses!", x=80, y=10)
  27.  
  28. # display the image drawing
  29. self.statbmp.SetBitmap(image)
  30.  
  31. def save_image(self):
  32. """save the drawing"""
  33. finished_image = self.statbmp.GetBitmap()
  34. #finished_image.SaveFile("myimage.png", wx.BITMAP_TYPE_PNG)
  35. finished_image.SaveFile("myimage.jpg", wx.BITMAP_TYPE_JPEG)
  36.  
  37.  
  38. app = wx.App(0)
  39. MyFrame(title='draw on an image and save it').Show()
  40. app.MainLoop()
Attached Thumbnails
roses.jpg  
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,946
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 913
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Starting wxPython (GUI code)

 
0
  #39
Jul 21st, 2008
In case you would ever need to fool with the wx.Frame itself. The wx.Frame default style is wx.DEFAULT_FRAME_STYLE and is normally defined as:
wx.MINIMIZE_BOX|wx.MAXIMIZE_BOX|wx.RESIZE_BORDER|wx.SYSTEM_MENU|wx.CAPTION|
wx.CLOSE_BOX|wx.CLIP_CHILDREN
so remove wx.MAXIMIZE_BOX to disable it ...
  1. # a wx.Frame with the max button/box disabled
  2.  
  3. import wx
  4.  
  5. class MyFrame(wx.Frame):
  6. def __init__(self, parent, mytitle, mysize):
  7. wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle, size=mysize,
  8. style=wx.MINIMIZE_BOX|wx.RESIZE_BORDER|wx.SYSTEM_MENU|
  9. wx.CAPTION|wx.CLOSE_BOX|wx.CLIP_CHILDREN)
  10.  
  11.  
  12. app = wx.App(0)
  13. # create a MyFrame instance and show the frame
  14. MyFrame(None, 'Max box disabled', (400, 300)).Show()
  15. app.MainLoop()
Here is an example of a frame without a border or title bar. Note that you have to supply your own exit button ...
  1. # wx.Frame with no title bar
  2.  
  3. import wx
  4.  
  5. def exit(event):
  6. frame.Close(True)
  7.  
  8. app = wx.App(0)
  9. # create a window, no-parent, -1 is default ID, style with no titlebar
  10. frame = wx.Frame(parent=None, id=-1, pos=(50,100), size=(300,200),
  11. style=wx.MINIMIZE_BOX)
  12. frame.SetBackgroundColour('green')
  13.  
  14. # provide exit for a frame without titlebar
  15. quit = wx.Button(frame, id=-1, label='Exit', pos=(0,175))
  16. quit.Bind(wx.EVT_BUTTON, exit)
  17.  
  18. # show the window
  19. frame.Show(True)
  20.  
  21. # start the event loop
  22. app.MainLoop()
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,946
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 913
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Starting wxPython (GUI code)

 
0
  #40
Jul 21st, 2008
Show one of those ever popular animated gif images Zoe style ...
  1. # use wx.animate.GIFAnimationCtrl() to show an animated gif
  2.  
  3. import wx
  4. import wx.animate # ..\wx\animate.py
  5.  
  6. app = wx.App(0)
  7. frame = wx.Frame(None, wx.ID_ANY, "Show an animated gif",
  8. size=(250, 150))
  9. frame.SetBackgroundColour("white")
  10.  
  11. # pick an animated GIF file you have in the working directory
  12. # (give it the full path if located in another directory)
  13. ag_fname = 'AG_Dog.gif'
  14. # create the instance with this file name
  15. ag = wx.animate.GIFAnimationCtrl(frame, wx.ID_ANY, ag_fname)
  16. # clear the background
  17. ag.GetPlayer().UseBackgroundColour(True)
  18. # continuously loop through the frames of the gif file (default)
  19. ag.Play()
  20.  
  21. frame.Show()
  22. app.MainLoop()
Attached Images
 
May 'the Google' be with you!
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