Starting wxPython (GUI code)

Reply

Join Date: Jun 2007
Posts: 1,333
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 124
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso

Re: Starting wxPython (GUI code)

 
0
  #101
Oct 31st, 2008
I was in search for wx_IDs and one guy presented me with some code with for loop!
I modified a little bit and here is a code for simple preogram that will show all available built in wx.IDs
  1. import wx
  2.  
  3. class Frame1(wx.Frame):
  4. def __init__ (self, parent, id, title):
  5. wx.Frame.__init__(self, parent, id, title, size=(400, 400))
  6. self.text = wx.TextCtrl(self, -1, style = wx.TE_MULTILINE)
  7.  
  8. x = dir(wx)
  9.  
  10. for y in x:
  11. if y.find('ID_')!=-1:
  12. search = y + "\n"
  13. self.text.write(search)
  14.  
  15.  
  16. app = wx.App(False)
  17. f = Frame1(None, -1, "wx.IDs")
  18. f.Centre()
  19. f.Show(True)
  20. app.MainLoop()
Editor's note:
Careful when copying this code to your editor, it uses mixed spaces and tabs.
Last edited by vegaseat; Jan 1st, 2009 at 3:45 pm. Reason: mixed spaces and tabs problems
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 895
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 143
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345

Re: Starting wxPython (GUI code)

 
0
  #102
Nov 2nd, 2008
If you ever need the values of wx ID's as well as any other constants in wxPython here is a way to do it:
  1. import wx
  2.  
  3. class MainFrame(wx.Frame):
  4. def __init__ (self):
  5. wx.Frame.__init__(self,None,title = "ID's and Values", size=(400, 400))
  6. self.text = wx.TextCtrl(self, wx.ID_ANY, style = wx.TE_MULTILINE)
  7.  
  8.  
  9. for f in wx.__dict__:
  10. if f.startswith("ID_"):
  11. t = f+' --> '+str(wx.__dict__[f])+'\n'
  12. self.text.write(t)
  13. self.Show()
  14.  
  15.  
  16. app = wx.App(False)
  17. f = MainFrame()
  18. app.MainLoop()
If you want a full dictionary of values try this:

  1. import wx
  2.  
  3. class MainFrame(wx.Frame):
  4. def __init__ (self):
  5. wx.Frame.__init__(self,None,title = "ID's and Values", size=(400, 400))
  6. self.text = wx.TextCtrl(self, wx.ID_ANY, style = wx.TE_MULTILINE)
  7.  
  8. for f in wx.__dict__:
  9. try:
  10. t = f+' --> '+str(int(wx.__dict__[f]))+'\n'
  11. self.text.write(t)
  12. except:
  13. pass
  14.  
  15. self.Show()
  16.  
  17.  
  18. app = wx.App(False)
  19. f = MainFrame()
  20. app.MainLoop()
Notive that bit where is goes str(int(wx.__dict__[f])) ? Thats to make sure that anything that has a value that isnt an int will not work as there will be an error and the program will skip it.
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,273
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
  #103
Dec 23rd, 2008
The wx.RichTextCtrl() widget allows the user to create formatted text with color, size, font, bullets, images and more:
  1. # experiment with wxPython's
  2. # wx.RichTextCtrl(parent, id, value, pos, size, style=wx.RE_MULTILINE,
  3. # validator, name)
  4. # allows you to create formatted text with color, size, font, images ...
  5. # snee
  6.  
  7. import wx
  8. import wx.richtext
  9.  
  10. class MyFrame(wx.Frame):
  11. def __init__(self, parent, mytitle, mysize):
  12. wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle, size=mysize)
  13. self.SetBackgroundColour("white")
  14.  
  15. self.rich = wx.richtext.RichTextCtrl(self, wx.ID_ANY, value="")
  16. self.rich.WriteText("Default is black text.\n")
  17. self.rich.BeginBold()
  18. self.rich.WriteText("Write this text in bold")
  19. self.rich.BeginTextColour('red')
  20. self.rich.WriteText(" and this text in bold and red.\n")
  21. self.rich.EndTextColour()
  22. self.rich.EndBold()
  23.  
  24. self.rich.BeginFontSize(30)
  25. self.rich.WriteText("This text has point size 30\n")
  26. self.rich.EndFontSize()
  27.  
  28. font = wx.Font(16, wx.SCRIPT, wx.NORMAL, wx.LIGHT)
  29. self.rich.BeginFont(font)
  30. self.rich.WriteText("This text has a different font\n")
  31. self.rich.EndFont()
  32. self.rich.Newline()
  33. # indent the next items 100 units (tenths of a millimeter)
  34. self.rich.BeginLeftIndent(100)
  35.  
  36. # insert an image you have in the work directory
  37. # or give the full path, can be .bmp .gif .png .jpg
  38. image_file = "Duck2.jpg"
  39. image= wx.Image(image_file, wx.BITMAP_TYPE_ANY)
  40. # wx.BITMAP_TYPE_ANY tries to autodetect the format
  41. self.rich.WriteImage(image)
  42. self.rich.Newline()
  43. self.rich.EndLeftIndent()
  44.  
  45.  
  46. app = wx.App()
  47. mytitle = 'testing wx.RichTextCtrl'
  48. width = 560
  49. height = 400
  50. # create the MyFrame instance and show the frame
  51. MyFrame(None, mytitle, (width, height)).Show()
  52. app.MainLoop()
Attached Thumbnails
Duck2.jpg  
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,959
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: 918
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Starting wxPython (GUI code)

 
0
  #104
Jan 13th, 2009
An example on how to pick the rgb colour value on a wx.PaintDC canvas with GetPixel(x, y) ...
  1. # draw a colourful rectangle on a wx.PaintDC surface
  2. # and get the colour at the point of a mouse click
  3. # using wx.PaintDC's GetPixel(x, y)
  4. # tested with Python25 and wxPython28 by 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. # this will also be the canvas surface color
  12. self.SetBackgroundColour('white')
  13. # bind the frame to the paint event
  14. wx.EVT_PAINT(self, self.onPaint)
  15. # bind the left mouse button click event
  16. self.Bind(wx.EVT_LEFT_DOWN, self.onLeftDown)
  17.  
  18. def onPaint(self, event=None):
  19. # this is the wxPython drawing surface/canvas
  20. self.dc = wx.PaintDC(self)
  21. # create a concentric gradient fill area
  22. # rect has upper left corner coordinates (x, y)
  23. x = 10
  24. y = 10
  25. width = 450
  26. height = 450
  27. center = ((width-x)/2, (height-y)/2)
  28. self.dc.GradientFillConcentric((x, y, width, height),
  29. 'red', 'blue', center)
  30.  
  31. def onLeftDown(self, event):
  32. """left mouse button is pressed"""
  33. # get the (x, y) position tuple
  34. x, y = event.GetPosition()
  35. # get (r,g,b) colour of pixel at point (x,y)
  36. r, g, b = self.dc.GetPixel(x, y)[:3]
  37. # show result in frame's titlebar
  38. s = "Colour at (x=%s,y=%s) is (r=%s,g=%s,b=%s)" % (x, y, r, g, b)
  39. self.SetTitle(s)
  40.  
  41.  
  42. app = wx.App()
  43. mytitle = "click on a point to show colour"
  44. width = 480
  45. height = 510
  46. MyFrame(None, mytitle, (width, height)).Show()
  47. app.MainLoop()
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 895
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 143
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345

Re: Starting wxPython (GUI code)

 
0
  #105
Jan 17th, 2009
When using a richTextCtrl on a Panel often comes up with problems such as when you press Enter\Return to make a new line nothing happens. Here is an example to show people who have no idea what problem i am taking about. Try using this code and then making a new line. It will not work.
  1. iimport wx
  2. import wx.richtext as rc
  3.  
  4. class MainFrame(wx.Frame):
  5. def __init__(self):
  6. wx.Frame.__init__(self, None, size = (120,140))
  7. self.panel = wx.Panel(self) #Notice you cant press enter and get a new line?
  8.  
  9. self.rich = rc.RichTextCtrl(self.panel, size = (100,100))
  10. self.Show()
  11.  
  12.  
  13. app = wx.App(0)
  14. frame = MainFrame()
  15. app.MainLoop()

But to fix it! Just add a style of 0 to the panel!

  1. import wx
  2. import wx.richtext as rc
  3.  
  4. class MainFrame(wx.Frame):
  5. def __init__(self):
  6. wx.Frame.__init__(self, None, size = (120,140))
  7. self.panel = wx.Panel(self, style = 0) #Yay it works
  8.  
  9. self.rich = rc.RichTextCtrl(self.panel, size = (100,100))
  10. self.Show()
  11.  
  12.  
  13. app = wx.App(0)
  14. frame = MainFrame()
  15. app.MainLoop()

Hope that helps anyone that had that problem, i know it stumped me for hours!
Last edited by Paul Thompson; Jan 17th, 2009 at 2:41 am.
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,273
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)

 
0
  #106
Jan 19th, 2009
One way to present highly formatted text like text with superscripts is to use wxPython's wx.html.HtmlWindow widget and simplified html code:
  1. # exploring wxPython's
  2. # wx.html.HtmlWindow(parent, id, pos, size, style, name)
  3.  
  4. import wx
  5. import wx.html
  6.  
  7. class MyPanel(wx.Panel):
  8. def __init__(self, parent):
  9. wx.Panel.__init__(self, parent, wx.ID_ANY)
  10. self.SetBackgroundColour("yellow")
  11.  
  12. htmlwin = wx.html.HtmlWindow(self, wx.ID_ANY,
  13. pos=(10,30), size=(200,100))
  14. htmlwin.SetPage(html_code)
  15.  
  16.  
  17. # use simple HTML code ...
  18. html_code = """\
  19. x<sup>3</sup> + y<sup>2</sup> - 15 = 0
  20. """
  21.  
  22. app = wx.App(0)
  23. # create a frame, no parent, default ID, title, size
  24. caption = "superscript text with wx.html.HtmlWindow"
  25. frame = wx.Frame(None, wx.ID_ANY, caption, size=(400, 210))
  26. MyPanel(frame)
  27. frame.Show()
  28. app.MainLoop()
Attached Thumbnails
htmlwindow3.jpg  
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,523
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)

 
0
  #107
Feb 14th, 2009
The wx.html.HtmlWindow widget is also very useful to show text in changing size and colour:
  1. # using wxPython's
  2. # wx.html.HtmlWindow(parent, id, pos, size, style, name)
  3. # to show colourful text using relatively simple html code
  4.  
  5. import wx
  6. import wx.html
  7.  
  8. class MyFrame(wx.Frame):
  9. def __init__(self, parent, mytitle, mysize, html_code1, html_code2):
  10. wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle,
  11. size=mysize)
  12.  
  13. # create an html label/window
  14. htmlwin1 = wx.html.HtmlWindow(self, wx.ID_ANY)
  15. htmlwin1.SetPage(html_code1)
  16.  
  17. htmlwin2 = wx.html.HtmlWindow(self, wx.ID_ANY)
  18. htmlwin2.SetPage(html_code2)
  19.  
  20. # position the labels with a box sizer
  21. sizer_v = wx.BoxSizer(wx.VERTICAL)
  22. sizer_v.Add(htmlwin1, proportion=1, flag=wx.EXPAND)
  23. sizer_v.Add(htmlwin2, proportion=2, flag=wx.EXPAND)
  24. self.SetSizer(sizer_v)
  25.  
  26.  
  27. # simple HTML code ...
  28. # text between <B> and </B> is bold
  29. # <BR> inserts a line break (or new line)
  30. # text between <FONT color="blue"> and </FONT> is that color
  31. # add size to the FONT tag
  32. html_code1 = """\
  33. This shows you how to display text in color
  34. <BR>
  35. <FONT color="blue">like blue text</FONT>
  36. <FONT color="red"> or red text</FONT>
  37. <B> or maybe just bold ...</B>
  38. <BR><BR>
  39. <FONT color="green" size=5>
  40. You are in extreme
  41. <FONT color="red" size=5>danger</FONT>
  42. standing so close!
  43. </FONT>
  44. """
  45.  
  46. # a few more HTML tags ...
  47. # use <BODY> tags to add a background colour
  48. # text between <H3> and </H3> is header size
  49. # etc. etc. just experiment with the <> tags
  50. html_code2 = """\
  51. <BODY bgcolor="#FFE47E">
  52. look at the new background colour ...
  53. <H3>large header size</H3><H2>larger header size</H2>
  54. <BR>
  55. <FONT size="+4">even larger font size</FONT>
  56. <BR><BR>
  57. <FONT color="red" size="+4">larger </FONT>
  58. <FONT color="green" size="+4">size </FONT>
  59. <FONT color="blue" size="+4">and </FONT>
  60. <FONT color="magenta" size="+4">color</FONT>
  61. <BR>
  62. </BODY>
  63. """
  64.  
  65. app = wx.App()
  66. mytitle = "wx.html.HtmlWindow for HTML formatted text"
  67. width = 450
  68. height = 350
  69. frame = MyFrame(None, mytitle, (width, height), html_code1, html_code2)
  70. frame.Show(True)
  71. frame.Center()
  72. app.MainLoop()
  73.  
Attached Thumbnails
html_label.jpg  
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,959
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: 918
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Starting wxPython (GUI code)

 
0
  #108
May 20th, 2009
If you want to delay a function call in wxPython it is best to use function wx.FutureCall(delaytime, function) ...
  1. # use wx.FutureCall() to call a function after a set time
  2. # allows present process to go on
  3.  
  4. import wx
  5.  
  6. class MyFrame(wx.Frame):
  7. def __init__(self, parent, mytitle):
  8. wx.Frame.__init__(self, parent, -1, mytitle, size=(450, 300))
  9. self.SetBackgroundColour('red')
  10. # call change_colour() after 2000 milliseconds (2 seconds)
  11. wx.FutureCall(2000, self.change_colour)
  12.  
  13. def change_colour(self):
  14. self.SetBackgroundColour('green')
  15. self.ClearBackground()
  16.  
  17.  
  18. app = wx.App(0)
  19. MyFrame(None, 'change colour to green after 2 seconds').Show()
  20. app.MainLoop()
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,959
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: 918
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Starting wxPython (GUI code)

 
0
  #109
May 20th, 2009
This little program will browse for a sound file and then play it when you click the play button ...
  1. # exploring wxPython's
  2. # wx.Sound(fileName, isResource=False) and
  3. # wx.lib.filebrowsebutton.FileBrowseButton(parent,labelText,fileMask)
  4. # vega
  5.  
  6. import wx
  7. import wx.lib.filebrowsebutton
  8.  
  9. class MyFrame(wx.Frame):
  10. def __init__(self, parent, mytitle):
  11. wx.Frame.__init__(self, parent, -1, mytitle, size=(600, 90))
  12. self.SetBackgroundColour("green")
  13. panel = wx.Panel(self)
  14.  
  15. self.fbb = wx.lib.filebrowsebutton.FileBrowseButton(panel,
  16. labelText="Browse for a WAVE file:", fileMask="*.wav")
  17. play_button = wx.Button(panel, -1, " Play ")
  18. self.Bind(wx.EVT_BUTTON, self.onPlay, play_button)
  19.  
  20. # setup the layout with sizers
  21. # line up widgets horizontally
  22. hsizer = wx.BoxSizer(wx.HORIZONTAL)
  23. hsizer.Add(self.fbb, 1, wx.ALIGN_CENTER_VERTICAL)
  24. hsizer.Add(play_button, 0, wx.ALIGN_CENTER_VERTICAL)
  25.  
  26. # create a border space
  27. border = wx.BoxSizer(wx.VERTICAL)
  28. border.Add(hsizer, 0, flag=wx.ALL|wx.EXPAND, border=10)
  29. # only neeed to set this sizer since it contains the hsizer
  30. panel.SetSizer(border)
  31.  
  32. def onPlay(self, event):
  33. """the play button has been clicked"""
  34. filename = self.fbb.GetValue()
  35. sound = wx.Sound(filename)
  36. sound.Play(wx.SOUND_ASYNC)
  37.  
  38.  
  39. app = wx.App(0)
  40. mytitle = "wx.lib.filebrowsebutton.FileBrowseButton and wx.Sound"
  41. MyFrame(None, mytitle).Show()
  42. app.MainLoop()
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,523
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)

 
0
  #110
May 24th, 2009
Let's get a little playful and display a text in rainbow colors:
  1. # use wxPython's wx.richtext.RichTextCtrl
  2. # to create rainbow colored text
  3.  
  4. import wx
  5. import wx.richtext
  6.  
  7. class MyFrame(wx.Frame):
  8. def __init__(self, parent, title, rainbow_text):
  9. wx.Frame.__init__(self, parent, -1, title, size=(560, 220))
  10. self.SetBackgroundColour("white")
  11.  
  12. self.rich = wx.richtext.RichTextCtrl(self, -1, value="")
  13. self.rich.BeginFontSize(40)
  14. for c, colour in rainbow_text:
  15. self.rich.BeginTextColour(colour)
  16. self.rich.WriteText(c)
  17. self.rich.EndTextColour()
  18.  
  19.  
  20. rainbow = ['red', 'coral', 'yellow', 'green', 'blue']
  21.  
  22. text = """
  23. Welcome to fabulous
  24. Las Vegas"""
  25.  
  26. # create a list of (char, colour) tuples
  27. rainbow_text = []
  28. ix = 0
  29. for c in text:
  30. if c.isalpha():
  31. colour = rainbow[ix]
  32. if ix < len(rainbow)-1:
  33. ix += 1
  34. else:
  35. ix = 0
  36. else:
  37. colour = 'white'
  38. rainbow_text.append((c, colour))
  39.  
  40. app = wx.App(0)
  41. title = 'Rainbow Text'
  42. MyFrame(None, title, rainbow_text).Show()
  43. app.MainLoop()
Last edited by Ene Uran; May 24th, 2009 at 7:14 pm.
Attached Thumbnails
wxrichtext1.jpg  
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