943,718 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 87703
  • Python RSS
You are currently viewing page 12 of this multi-page discussion thread; Jump to the first page
Jun 13th, 2009
2

Re: Starting wxPython (GUI code)

I think we may have talked about this before, but I can't find it. Here is a way to use a sound file inside the Python code and play it. First create a base64 encoded string of the binary sound file:
python Syntax (Toggle Plain Text)
  1. # create a base64 encoded string from binary sound data
  2. # and limit the length of each data line to n characters
  3. # ene
  4.  
  5. import base64
  6.  
  7. def text_lines(data, n):
  8. """
  9. write a long one line text as lines of n char each
  10. """
  11. s = ""
  12. for ix, c in enumerate(data):
  13. if ix % n == 0 and ix != 0:
  14. s += c + '\n'
  15. else:
  16. s += c
  17. return s
  18.  
  19. # pick a wave file that is in the working folder
  20. wave_file = "pop.wav"
  21. # this is a binary read
  22. data = base64.b64encode(open(wave_file, 'rb').read())
  23. data = text_lines(data, 70)
  24. print "wav_base64='''\\\n" + data + "'''"
  25.  
  26. """
  27. highlight and copy the resulting base64 wav string to embed in program
  28. code, use wav = base64.b64decode(wav_base64) to decode to binary data:
  29.  
  30. wav_base64='''\
  31. UklGRuABAABXQVZFZm10IBAAAAABAAEAESsAABErAAABAAgAZGF0YbsBAACAgICAgICAgIC
  32. AgICAfX12aVtQS0hJSk5TZpWyv8LBwMC/vry2nWxLPTs9PT5AR05pn77BwcC/vruxlGtJP
  33. T09PkBHU3qrv8DAv7+7tKiGWUA8PT4+Qk1tm7fAv7++ubKmhl1CPD0+P0NPbZayv8C/vbe
  34. vooNdRD0+PkBIWXWVrru/vbmypo5uU0I+P0NLWG6JorO7vLm0rJyBZlBEQkNIUF5xiJ2wu
  35. bq4s6qZg21ZTUhITFRhcoaaqLGzsayilIFvYFVQUFNZY29/j52orKunnpKFdmheWFVWWmF
  36. seYiXoqmrqaKXi390amRfXmBka3aDj5ifoqGemZGIfXJpY2FiZmx0fYePlpqcmpSNhHpyb
  37. Wloam91foaNk5eXlZGMhH12b2xqam1xd32Ei5CVlpSPiYF6dHFvbnByd36Fi4+SkY6KhoN
  38. +end3d3h8gYaKjIyMioeDf3t4dnZ3eX2BhYiKi4uJhoJ+eXZ1dnd6fICFiY2PjoyHgnx3d
  39. XNzdHZ6f4WKjpCPjIiEf3t3dHNzdXh8goeLjY2NiYWBfHh0cnJzdnl9gYSGh4aFg398eXd
  40. 2d3l8foCChIaIh4aEgQA='''
  41.  
  42. """
Now you can transfer the result to your Python program and play the sound without having to attach the sound file:
python Syntax (Toggle Plain Text)
  1. # playing a base64 encoded wave sound string with wxPython's
  2. # wx.SoundFromData(sound_data) and sound.Play()
  3. # ene
  4.  
  5. import wx
  6. import base64
  7.  
  8. class MyFrame(wx.Frame):
  9. def __init__(self, parent, mytitle, mysize, sound_data):
  10. wx.Frame.__init__(self, parent, -1, mytitle, size=mysize)
  11. self.SetBackgroundColour("blue")
  12. # use a small delay to display the frame before sound starts
  13. wx.FutureCall(100, self.play_sound)
  14.  
  15. def play_sound(self):
  16. sound = wx.SoundFromData(sound_data)
  17. # wx.SOUND_SYNC --> sound plays completely through
  18. for k in range(10):
  19. sound.Play(wx.SOUND_SYNC)
  20.  
  21.  
  22. wav_base64='''\
  23. UklGRuABAABXQVZFZm10IBAAAAABAAEAESsAABErAAABAAgAZGF0YbsBAACAgICAgICAgIC
  24. AgICAfX12aVtQS0hJSk5TZpWyv8LBwMC/vry2nWxLPTs9PT5AR05pn77BwcC/vruxlGtJP
  25. T09PkBHU3qrv8DAv7+7tKiGWUA8PT4+Qk1tm7fAv7++ubKmhl1CPD0+P0NPbZayv8C/vbe
  26. vooNdRD0+PkBIWXWVrru/vbmypo5uU0I+P0NLWG6JorO7vLm0rJyBZlBEQkNIUF5xiJ2wu
  27. bq4s6qZg21ZTUhITFRhcoaaqLGzsayilIFvYFVQUFNZY29/j52orKunnpKFdmheWFVWWmF
  28. seYiXoqmrqaKXi390amRfXmBka3aDj5ifoqGemZGIfXJpY2FiZmx0fYePlpqcmpSNhHpyb
  29. Wloam91foaNk5eXlZGMhH12b2xqam1xd32Ei5CVlpSPiYF6dHFvbnByd36Fi4+SkY6KhoN
  30. +end3d3h8gYaKjIyMioeDf3t4dnZ3eX2BhYiKi4uJhoJ+eXZ1dnd6fICFiY2PjoyHgnx3d
  31. XNzdHZ6f4WKjpCPjIiEf3t3dHNzdXh8goeLjY2NiYWBfHh0cnJzdnl9gYSGh4aFg398eXd
  32. 2d3l8foCChIaIh4aEgQA='''
  33.  
  34. sound_data = base64.b64decode(wav_base64)
  35.  
  36. app = wx.App(0)
  37. # create a MyFrame instance and show the frame
  38. MyFrame(None, 'wx.SoundFromData()', (300, 100), sound_data).Show()
  39. app.MainLoop()
Last edited by Ene Uran; Jun 13th, 2009 at 7:49 pm.
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005
Jun 27th, 2009
0

Re: Starting wxPython (GUI code)

I was doing a search on Daniweb for 'wx.AboutBox()', but it simply did not select a post, only the whole thread. So, this may be already somewhere. I am testing wxPython's fancy wx.AboutBox widget:
python Syntax (Toggle Plain Text)
  1. # testing the fancy wx.AboutBox() widget
  2. # snee
  3.  
  4. import wx
  5. from wx.lib.wordwrap import wordwrap
  6.  
  7. class MyFrame(wx.Frame):
  8. """
  9. create a frame, with a menu, statusbar and 2 about dialogs
  10. """
  11. def __init__(self):
  12. # create a frame/window, no parent, default to wxID_ANY
  13. wx.Frame.__init__(self, None, wx.ID_ANY, "wx.AboutBox test",
  14. pos=(300, 150), size=(300, 350))
  15. self.SetBackgroundColour("brown")
  16.  
  17. # create a status bar at the bottom
  18. self.CreateStatusBar()
  19. self.SetStatusText("Click on File")
  20.  
  21. menu = wx.Menu()
  22. # the optional & allows you to use alt/a
  23. # the last string argument shows in the status bar on mouse_over
  24. menu_about = menu.Append(wx.ID_ANY, "&About", "Ho-hum about box")
  25. menu_about2 = menu.Append(wx.ID_ANY, "About&2", "Fancy about box")
  26. menu.AppendSeparator()
  27. # the optional & allows you to use alt/x
  28. menu_exit = menu.Append(wx.ID_ANY, "E&xit", "Quit the program")
  29.  
  30. # create a menu bar at the top
  31. menuBar = wx.MenuBar()
  32. # the & allows you to use alt/f
  33. menuBar.Append(menu, "&File")
  34. self.SetMenuBar(menuBar)
  35.  
  36. # bind the menu events to an action/function/method
  37. self.Bind(wx.EVT_MENU, self.onMenuAbout, menu_about)
  38. self.Bind(wx.EVT_MENU, self.onMenuAbout2, menu_about2)
  39. self.Bind(wx.EVT_MENU, self.onMenuExit, menu_exit)
  40.  
  41. def onMenuAbout(self, event):
  42. """a somewhat ho-hum about box"""
  43. dlg = wx.MessageDialog(self,
  44. "a simple application using wxFrame, wxMenu\n"
  45. "a statusbar, and this about message.",
  46. "About", wx.OK | wx.ICON_INFORMATION)
  47. dlg.ShowModal()
  48. dlg.Destroy()
  49.  
  50. def onMenuAbout2(self, event):
  51. """use the much fancier wx.AboutBox()"""
  52. # first fill the info object
  53. info = wx.AboutDialogInfo()
  54. # Name and Version show in larger font
  55. info.Name = "Bratwurst7"
  56. info.Version = "v.1.7.4"
  57. info.Copyright = "(C) copyfight 2008"
  58. info.Description = wordwrap(
  59. "The Bratwurst7 program is a software program that "
  60. "makes you desire a freshly grilled bratwurst and "
  61. "a good German beer right now! Teaching programmers "
  62. "everywhere to be aware of those hidden immediate "
  63. "inner desires!",
  64. 300, wx.ClientDC(self))
  65. info.WebSite = ("http://en.wikipedia.org/wiki/Bratwurst",
  66. "Bratwurst7 home")
  67. info.Developers = ["Carl Arm", "Carol Bein", "Candy Kisser"]
  68. info.License = "Wish upon a star!"
  69. # now call wx.AboutBox with this info object
  70. wx.AboutBox(info)
  71.  
  72. def onMenuExit(self, event):
  73. self.Close(True)
  74.  
  75.  
  76. app = wx.App()
  77. # create the MyFrame class instance, then show the frame
  78. MyFrame().Show()
  79. app.MainLoop()
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Jul 17th, 2009
0

Re: Starting wxPython (GUI code)

This code shows how to create a custom pop-up dialog for text entry ...
python Syntax (Toggle Plain Text)
  1. # using wxPython's
  2. # wx.Dialog(parent, id, title, pos, size, style, name="dialogBox")
  3. # to create a custom text entry pop-up dialog
  4. # vegaseat
  5.  
  6. import wx
  7.  
  8. class MyFrame(wx.Frame):
  9. def __init__(self, parent, mytitle, mysize):
  10. wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle, size=mysize)
  11. self.SetBackgroundColour("green")
  12.  
  13. # call the dialog or input widget
  14. self.button = wx.Button(self, -1, label='Get the name', pos=(20, 20))
  15. # bind mouse or key event to an action
  16. self.button.Bind(wx.EVT_BUTTON, self.onAction)
  17. # create an output widget
  18. self.result = wx.TextCtrl(self, -1, value="", pos=(20, 70),
  19. size=(200, 120), style=wx.TE_MULTILINE|wx.HSCROLL)
  20.  
  21. def onAction(self, event):
  22. """call the pop-up dialog and get the string entered"""
  23. dlg = MyDialog(self, 'Text Entry', 'Enter your name:')
  24. if dlg.ShowModal() == wx.ID_OK:
  25. # note, parent has been assigned dialog's entry instance
  26. self.name = self.entry.GetValue()
  27. self.result.AppendText(self.name + '\n')
  28. dlg.Destroy()
  29.  
  30.  
  31. class MyDialog(wx.Dialog):
  32. def __init__(self, parent, mytitle, msg):
  33. wx.Dialog.__init__(self, parent, -1, mytitle, size=(250,180))
  34. entry = wx.TextCtrl(self, -1, value="", size=(200, 20))
  35. # assign dialog's entry instance to parent
  36. parent.entry = entry
  37. button_ok = wx.Button(self, wx.ID_OK)
  38. button_cancel = wx.Button(self, wx.ID_CANCEL)
  39. sizer = self.CreateTextSizer(' ' + msg)
  40. sizer.Add(entry, 0, wx.ALL, 5)
  41. sizer.Add(button_ok, 0, wx.ALL, 5)
  42. sizer.Add(button_cancel, 0, wx.ALL, 5)
  43. self.SetSizer(sizer)
  44.  
  45.  
  46. app = wx.App(0)
  47. # create a MyFrame instance and show the frame
  48. mytitle = 'custom entry dialog'
  49. width = 300
  50. height = 260
  51. MyFrame(None, mytitle, (width, height)).Show()
  52. app.MainLoop()
Last edited by vegaseat; Jul 17th, 2009 at 10:48 pm.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jul 26th, 2009
0

Re: Starting wxPython (GUI code)

Here we use the simple wx.lib.filebrowsebutton.FileBrowseButton() to load and show an image ...
python Syntax (Toggle Plain Text)
  1. # experiment with wxPython's
  2. # wx.StaticBitmap(parent, id=-1, bitmap, pos=wx.DefaultPosition,
  3. # size=wx.DefaultSize, style=0, name="staticBitmap")
  4. # wx.lib.filebrowsebutton.FileBrowseButton(parent, labelText,
  5. # fileMask)
  6. # to load and show an image file
  7. # the frame expands to show the entire image
  8. # vegaseat
  9.  
  10. import wx
  11. import wx.lib.filebrowsebutton as wxfbb
  12.  
  13. class MyFrame(wx.Frame):
  14. def __init__(self, parent, mysize):
  15. wx.Frame.__init__(self, parent, -1, size=mysize)
  16. self.panel = wx.Panel(self)
  17. self.size = mysize
  18.  
  19. # file browser masked to look for common image types
  20. mask ="*.gif; *.jpg; *.png; *.bmp"
  21. self.fbb = wxfbb.FileBrowseButton(self.panel,
  22. labelText="Select an image file:", fileMask=mask)
  23. self.button = wx.Button(self.panel, wx.ID_ANY, ">> Show")
  24. self.button.Bind(wx.EVT_BUTTON, self.load_image)
  25. # setup the layout with sizers
  26. hsizer = wx.BoxSizer(wx.HORIZONTAL)
  27. hsizer.Add(self.fbb, 1, wx.ALIGN_CENTER_VERTICAL)
  28. hsizer.Add(self.button, 0, wx.ALIGN_CENTER_VERTICAL)
  29. # create a border space
  30. border = wx.BoxSizer(wx.VERTICAL)
  31. border.Add(hsizer, 0, wx.EXPAND|wx.ALL, 10)
  32. self.panel.SetSizer(border)
  33.  
  34. def load_image(self, event):
  35. image_file = self.fbb.GetValue()
  36. # hide the file browse panel
  37. self.panel.Hide()
  38.  
  39. image = wx.Bitmap(image_file)
  40. image_size = image.GetSize()
  41. # set the frame size to fit the image size
  42. self.SetClientSize(image_size)
  43.  
  44. # bitmap's upper left corner is frame pos=(0, 0) by default
  45. wx.StaticBitmap(self, wx.ID_ANY, image, size=image_size)
  46.  
  47. # optionally show some image information in the frame title
  48. width, height = image_size
  49. depth = image.GetDepth()
  50. info = "%s %dx%dx%d" % (image_file, width, height, depth)
  51. self.SetTitle(info)
  52.  
  53.  
  54. app = wx.App(0)
  55. # create a MyFrame instance and show the frame
  56. mysize = (600, 200)
  57. MyFrame(None, mysize).Show()
  58. app.MainLoop()
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jul 26th, 2009
0

Re: Starting wxPython (GUI code)

The wxPython wx.lib.fancytext.StaticFancyText() class can display colorful text written in XML format:
python Syntax (Toggle Plain Text)
  1. # wxPython's
  2. # wx.lib.fancytext.StaticFancyText(parent, id, text, ...)
  3. # can display text written in XML format
  4. # Henri
  5.  
  6. import wx
  7. import wx.lib.fancytext as wxft
  8.  
  9. class FancyText(wx.Panel):
  10. def __init__(self, parent, xml_text):
  11. wx.Panel.__init__(self, parent, -1)
  12. self.ftext = wxft.StaticFancyText(self, -1, text=xml_text,
  13. background=wx.Brush('black'))
  14.  
  15.  
  16. red = '<font family="swiss" color="red" size="48">'
  17. blue = '<font family="swiss" color="blue" size="48">'
  18. green = '<font family="swiss" color="green" size="48">'
  19.  
  20. # match the 3 <font ...> tags with closing </font> tags
  21. xml_text = """\
  22. %s Feeling %sblue?
  23. %s Or green with envy?
  24. </font>
  25. </font>
  26. </font>
  27. """ % (red, blue, green)
  28.  
  29. app = wx.App(0)
  30. mytitle = 'wx.lib.fancytext.StaticFancyText()'
  31. mysize = (620, 220)
  32. frame = wx.Frame(None, -1, title=mytitle, size=mysize)
  33. FancyText(frame, xml_text)
  34. frame.Show()
  35. app.MainLoop()
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Jul 29th, 2009
0

Re: Starting wxPython (GUI code)

Example of wx.ScrolledWindow to display large area in one small frame:
python Syntax (Toggle Plain Text)
  1. # test wx.ScrolledWindow(parent, id, pos, size, style)
  2. # with multiple widgets that exceed normal frame area
  3. # tested with Python 2.5.4 and wxPython 2.8.9.1
  4. # Henri
  5.  
  6. import wx
  7.  
  8. class MyFrame(wx.Frame):
  9. def __init__(self, parent, mytitle):
  10. wx.Frame.__init__(self, parent, -1, mytitle, size=(400,300))
  11.  
  12. # create scrolled window
  13. # let the wx.ScrolledWindow fill the frame
  14. self.scrollw = wx.ScrolledWindow(self, wx.ID_ANY)
  15. self.scrollw.SetBackgroundColour('green')
  16. # set EnableScrolling(bool x_scrolling, bool y_scrolling)
  17. # default is self.scrollw.EnableScrolling(True, True)
  18. # if True, wx.ScrolledWindow shows its scroll bars
  19. # create the scroll bars, set the scrolling ranges (pixels)
  20. max_w = 1000
  21. max_h = 1000
  22. # SetScrollbars(pixelsPerUnitX, pixelsPerUnitY, noUnitsX,
  23. # noUnitsY)
  24. self.scrollw.SetScrollbars(20, 20, max_w//20, max_h//20)
  25.  
  26. # pick something that needs area larger than the frame
  27. self.createMultiLabels()
  28.  
  29. def createMultiLabels(self):
  30. """from one of vegaseat's example codes"""
  31. # things to put on the labels
  32. label_string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ->UM'
  33. # wx.GridSizer(rows, cols, vgap, hgap)
  34. gsizer = wx.GridSizer(6, 5, 0, 0)
  35. # create list of labels
  36. # acccess with self.labels[0], self.labels[1] etc.
  37. self.labels = []
  38. for c in label_string:
  39. # labels have of scroll window as parent
  40. self.labels.append(wx.StaticText(self.scrollw, -1,
  41. label=c, style=wx.ALIGN_CENTRE|wx.SUNKEN_BORDER))
  42. # iterate through the list of labels and set
  43. # layout, optionally font and colour
  44. font = wx.Font(60, wx.MODERN, wx.NORMAL, wx.BOLD)
  45. for x in self.labels:
  46. x.SetFont(font)
  47. x.SetBackgroundColour("yellow")
  48. gsizer.Add(x, 0, flag=wx.ALL, border=20)
  49. # set the sizer in scroll window
  50. self.scrollw.SetSizer(gsizer)
  51.  
  52.  
  53. app = wx.App(0)
  54. # create MyFrame instance and show the frame
  55. MyFrame(None, "wx.ScrolledWindow for large display").Show()
  56. app.MainLoop()
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Jul 29th, 2009
0

Re: Starting wxPython (GUI code)

You can give your wxPython window nice custom icon:
python Syntax (Toggle Plain Text)
  1. # experiment with wxPython's wx.Icon() and SetIcon()
  2.  
  3. import wx
  4.  
  5. class MyFrame(wx.Frame):
  6. """make a frame, inherits wx.Frame"""
  7. def __init__(self):
  8. wx.Frame.__init__(self, None, -1, 'wxPython custom icon',
  9. pos=wx.Point(300, 150), size=wx.Size(300, 350))
  10. self.SetBackgroundColour('green')
  11.  
  12. # pick icon file (.ico) you have ...
  13. #iconFile = "py.ico"
  14. # or 32x32 Windows .bmp file will do ...
  15. # (will miss mask if not rectangular image)
  16. icon_file = "Attention32x32.bmp"
  17. self.SetIcon(wx.Icon(icon_file, wx.BITMAP_TYPE_ICO))
  18.  
  19. # show the frame
  20. self.Show(True)
  21.  
  22.  
  23. application = wx.App(0)
  24. # call class MyFrame
  25. window = MyFrame()
  26. application.MainLoop()
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Jul 29th, 2009
0

Re: Starting wxPython (GUI code)

Hey Henri, thanks for reminding me that these multilables are a nice filler to force the scrollbars to show. Here I applied them to the scrolled panel ...
python Syntax (Toggle Plain Text)
  1. # exploring wxPython's
  2. # wx.lib.scrolledpanel.ScrolledPanel(parent, id, pos, size, style)
  3. # default pos is (0, 0) and default size is (-1, -1) which fills the frame
  4. # the scrolled panel is an area on which other controls are placed
  5. # that can take more space then the parent frame has
  6.  
  7. import wx
  8. import wx.lib.scrolledpanel
  9.  
  10. class MyScrolledPanel(wx.lib.scrolledpanel.ScrolledPanel):
  11. def __init__(self, parent):
  12. # make the scrolled panel larger than its parent
  13. wx.lib.scrolledpanel.ScrolledPanel.__init__(self, parent,
  14. wx.ID_ANY, size=(600, 600), style=wx.TAB_TRAVERSAL)
  15. # scroll bars won't appear until required
  16. # default is SetupScrolling(scroll_x=True, scroll_y=True)
  17. self.SetupScrolling()
  18. self.SetBackgroundColour("blue")
  19.  
  20. # these multilabels will force the scroll bars to show
  21. # as they exceed the size of the frame
  22. label_string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ #@$'
  23. # wx.GridSizer(rows, cols, vgap, hgap)
  24. gsizer = wx.GridSizer(6, 5, 0, 0)
  25. # create a list of labels
  26. # acccess with self.labels[0], self.labels[1] etc.
  27. self.labels = []
  28. for c in label_string:
  29. self.labels.append(wx.StaticText(self, wx.ID_ANY,
  30. label=c, style=wx.ALIGN_CENTRE))
  31. # iterate through the list of labels and set layout
  32. # font and colour
  33. font = wx.Font(24, wx.MODERN, wx.NORMAL, wx.BOLD)
  34. for x in self.labels:
  35. x.SetFont(font)
  36. x.SetBackgroundColour("yellow")
  37. gsizer.Add(x, 0, flag=wx.ALL|wx.EXPAND, border=15)
  38. # set the sizer
  39. self.SetSizer(gsizer)
  40.  
  41.  
  42. app = wx.App(0)
  43. # create a frame, no parent, default ID, title, size
  44. caption = "ScrolledPanel"
  45. frame = wx.Frame(None, wx.ID_ANY, caption, size=(250, 310))
  46. MyScrolledPanel(frame)
  47. frame.Center()
  48. frame.Show()
  49. app.MainLoop()
Last edited by vegaseat; Aug 29th, 2009 at 7:30 pm. Reason: spelling
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Aug 29th, 2009
0

Re: Starting wxPython (GUI code)

If the user of your program has to select from a fixed list of choices, the wx.SingleChoiceDialog is a good way to go:
python Syntax (Toggle Plain Text)
  1. # exploring the wx.SingleChoiceDialog
  2. # fish_list from:
  3. # http://allrecipes.com/Recipes/Seafood/Fish/Main.aspx
  4. # snee
  5.  
  6. import wx
  7.  
  8. class MyFrame(wx.Frame):
  9. def __init__(self, fish_list):
  10. wx.Frame.__init__(self, None, -1, 'wx.SingleChoiceDialog',
  11. size=(350, 100))
  12. self.CreateStatusBar()
  13. label = wx.StaticText(self)
  14.  
  15. dlg = wx.SingleChoiceDialog(None,
  16. "What's your favorite fish to eat?",
  17. 'Select a Fish', fish_list)
  18. if dlg.ShowModal() == wx.ID_OK:
  19. answer = dlg.GetStringSelection()
  20. # show the selected choice different ways ...
  21. sf = 'Your choice was: %s' % answer
  22. label.SetLabel(sf)
  23. self.SetStatusText(sf)
  24. self.SetTitle(sf)
  25. dlg.Destroy()
  26.  
  27. fish_list = [
  28. 'Catfish',
  29. 'Cod',
  30. 'Flounder',
  31. 'Haddock',
  32. 'Halibut',
  33. 'Mahi Mahi',
  34. 'Salmon',
  35. 'Snapper',
  36. 'Swordfish',
  37. 'Tilapia',
  38. 'Trout',
  39. 'Tuna'
  40. ]
  41.  
  42. app = wx.App()
  43. frame = MyFrame(fish_list)
  44. frame.Center()
  45. frame.Show()
  46. app.MainLoop()
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Sep 15th, 2009
0

Re: Starting wxPython (GUI code)

So you want to test drive some wxPython widgets without all that OOP stuff, here are some examples how to do this:
python Syntax (Toggle Plain Text)
  1. # test a wxPython combobox selection
  2.  
  3. import wx
  4.  
  5. def combo_select(event):
  6. color = combo.GetValue()
  7. frame.SetTitle(color)
  8.  
  9.  
  10. app = wx.App(0)
  11. frame = wx.Frame(None, -1, "wx.ComboBox", size=(220, 40))
  12.  
  13. # set up the choices for the listbox part of the combobox
  14. choice_list = ['red', 'green', 'blue', 'yellow','white', 'magenta']
  15. # create a combobox widget
  16. combo = wx.ComboBox( frame, -1, choices=choice_list)
  17. # set initial value
  18. combo.SetValue('red')
  19. # click on a dropdown choice to select it
  20. combo.Bind(wx.EVT_COMBOBOX, combo_select)
  21.  
  22. frame.Center()
  23. frame.Show()
  24. app.MainLoop()
One more for the Gipper:
python Syntax (Toggle Plain Text)
  1. # test wxPython checkbox selections
  2.  
  3. import wx
  4.  
  5. def on_action(event):
  6. s = ""
  7. if cb1.IsChecked():
  8. s += "cb1 (red) is checked "
  9. if cb2.IsChecked():
  10. s += "cb2 (blue) is checked "
  11. if not cb1.IsChecked() and not cb2.IsChecked():
  12. s = "none is checked"
  13. frame.SetTitle(s)
  14.  
  15.  
  16. app = wx.App(0)
  17. frame = wx.Frame(None, -1, "wx.CheckBox", size=(450, 79))
  18.  
  19. cb1 = wx.CheckBox(frame, -1, 'red', pos=(10, 10))
  20. cb2 = wx.CheckBox(frame, -1, 'blue', pos=(10, 40))
  21. # set checkbox cb1 to checked
  22. cb1.SetValue(True)
  23. # bind checkbox mouse click to an action
  24. cb1.Bind(wx.EVT_CHECKBOX, on_action)
  25. cb2.Bind(wx.EVT_CHECKBOX, on_action)
  26. # initial call
  27. on_action(None)
  28.  
  29. frame.Center()
  30. frame.Show()
  31. app.MainLoop()
Last edited by sneekula; Sep 15th, 2009 at 6:00 pm.
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: wxpython image covering
Next Thread in Python Forum Timeline: Difficulty importing a file with a class definition





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC