| | |
Starting wxPython (GUI code)
![]() |
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
Editor's note:
Careful when copying this code to your editor, it uses mixed spaces and tabs.
I modified a little bit and here is a code for simple preogram that will show all available built in wx.IDs
python Syntax (Toggle Plain Text)
import wx class Frame1(wx.Frame): def __init__ (self, parent, id, title): wx.Frame.__init__(self, parent, id, title, size=(400, 400)) self.text = wx.TextCtrl(self, -1, style = wx.TE_MULTILINE) x = dir(wx) for y in x: if y.find('ID_')!=-1: search = y + "\n" self.text.write(search) app = wx.App(False) f = Frame1(None, -1, "wx.IDs") f.Centre() f.Show(True) app.MainLoop()
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
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
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:
If you want a full dictionary of values try this:
Notive that bit where is goes
python Syntax (Toggle Plain Text)
import wx class MainFrame(wx.Frame): def __init__ (self): wx.Frame.__init__(self,None,title = "ID's and Values", size=(400, 400)) self.text = wx.TextCtrl(self, wx.ID_ANY, style = wx.TE_MULTILINE) for f in wx.__dict__: if f.startswith("ID_"): t = f+' --> '+str(wx.__dict__[f])+'\n' self.text.write(t) self.Show() app = wx.App(False) f = MainFrame() app.MainLoop()
python Syntax (Toggle Plain Text)
import wx class MainFrame(wx.Frame): def __init__ (self): wx.Frame.__init__(self,None,title = "ID's and Values", size=(400, 400)) self.text = wx.TextCtrl(self, wx.ID_ANY, style = wx.TE_MULTILINE) for f in wx.__dict__: try: t = f+' --> '+str(int(wx.__dict__[f]))+'\n' self.text.write(t) except: pass self.Show() app = wx.App(False) f = MainFrame() app.MainLoop()
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
Check out my Site | and join us on IRC | Python Specific IRC
The wx.RichTextCtrl() widget allows the user to create formatted text with color, size, font, bullets, images and more:
python Syntax (Toggle Plain Text)
# experiment with wxPython's # wx.RichTextCtrl(parent, id, value, pos, size, style=wx.RE_MULTILINE, # validator, name) # allows you to create formatted text with color, size, font, images ... # snee import wx import wx.richtext class MyFrame(wx.Frame): def __init__(self, parent, mytitle, mysize): wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle, size=mysize) self.SetBackgroundColour("white") self.rich = wx.richtext.RichTextCtrl(self, wx.ID_ANY, value="") self.rich.WriteText("Default is black text.\n") self.rich.BeginBold() self.rich.WriteText("Write this text in bold") self.rich.BeginTextColour('red') self.rich.WriteText(" and this text in bold and red.\n") self.rich.EndTextColour() self.rich.EndBold() self.rich.BeginFontSize(30) self.rich.WriteText("This text has point size 30\n") self.rich.EndFontSize() font = wx.Font(16, wx.SCRIPT, wx.NORMAL, wx.LIGHT) self.rich.BeginFont(font) self.rich.WriteText("This text has a different font\n") self.rich.EndFont() self.rich.Newline() # indent the next items 100 units (tenths of a millimeter) self.rich.BeginLeftIndent(100) # insert an image you have in the work directory # or give the full path, can be .bmp .gif .png .jpg image_file = "Duck2.jpg" image= wx.Image(image_file, wx.BITMAP_TYPE_ANY) # wx.BITMAP_TYPE_ANY tries to autodetect the format self.rich.WriteImage(image) self.rich.Newline() self.rich.EndLeftIndent() app = wx.App() mytitle = 'testing wx.RichTextCtrl' width = 560 height = 400 # create the MyFrame instance and show the frame MyFrame(None, mytitle, (width, height)).Show() app.MainLoop()
No one died when Clinton lied.
An example on how to pick the rgb colour value on a wx.PaintDC canvas with GetPixel(x, y) ...
python Syntax (Toggle Plain Text)
# draw a colourful rectangle on a wx.PaintDC surface # and get the colour at the point of a mouse click # using wx.PaintDC's GetPixel(x, y) # tested with Python25 and wxPython28 by vegaseat import wx class MyFrame(wx.Frame): def __init__(self, parent, mytitle, mysize): wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle, size=mysize) # this will also be the canvas surface color self.SetBackgroundColour('white') # bind the frame to the paint event wx.EVT_PAINT(self, self.onPaint) # bind the left mouse button click event self.Bind(wx.EVT_LEFT_DOWN, self.onLeftDown) def onPaint(self, event=None): # this is the wxPython drawing surface/canvas self.dc = wx.PaintDC(self) # create a concentric gradient fill area # rect has upper left corner coordinates (x, y) x = 10 y = 10 width = 450 height = 450 center = ((width-x)/2, (height-y)/2) self.dc.GradientFillConcentric((x, y, width, height), 'red', 'blue', center) def onLeftDown(self, event): """left mouse button is pressed""" # get the (x, y) position tuple x, y = event.GetPosition() # get (r,g,b) colour of pixel at point (x,y) r, g, b = self.dc.GetPixel(x, y)[:3] # show result in frame's titlebar s = "Colour at (x=%s,y=%s) is (r=%s,g=%s,b=%s)" % (x, y, r, g, b) self.SetTitle(s) app = wx.App() mytitle = "click on a point to show colour" width = 480 height = 510 MyFrame(None, mytitle, (width, height)).Show() app.MainLoop()
May 'the Google' be with you!
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.
But to fix it! Just add a style of 0 to the panel!
Hope that helps anyone that had that problem, i know it stumped me for hours!
python Syntax (Toggle Plain Text)
iimport wx import wx.richtext as rc class MainFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, size = (120,140)) self.panel = wx.Panel(self) #Notice you cant press enter and get a new line? self.rich = rc.RichTextCtrl(self.panel, size = (100,100)) self.Show() app = wx.App(0) frame = MainFrame() app.MainLoop()
But to fix it! Just add a style of 0 to the panel!
python Syntax (Toggle Plain Text)
import wx import wx.richtext as rc class MainFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, size = (120,140)) self.panel = wx.Panel(self, style = 0) #Yay it works self.rich = rc.RichTextCtrl(self.panel, size = (100,100)) self.Show() app = wx.App(0) frame = MainFrame() 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
Check out my Site | and join us on IRC | Python Specific IRC
One way to present highly formatted text like text with superscripts is to use wxPython's wx.html.HtmlWindow widget and simplified html code:
python Syntax (Toggle Plain Text)
# exploring wxPython's # wx.html.HtmlWindow(parent, id, pos, size, style, name) import wx import wx.html class MyPanel(wx.Panel): def __init__(self, parent): wx.Panel.__init__(self, parent, wx.ID_ANY) self.SetBackgroundColour("yellow") htmlwin = wx.html.HtmlWindow(self, wx.ID_ANY, pos=(10,30), size=(200,100)) htmlwin.SetPage(html_code) # use simple HTML code ... html_code = """\ x<sup>3</sup> + y<sup>2</sup> - 15 = 0 """ app = wx.App(0) # create a frame, no parent, default ID, title, size caption = "superscript text with wx.html.HtmlWindow" frame = wx.Frame(None, wx.ID_ANY, caption, size=(400, 210)) MyPanel(frame) frame.Show() app.MainLoop()
No one died when Clinton lied.
The wx.html.HtmlWindow widget is also very useful to show text in changing size and colour:
python Syntax (Toggle Plain Text)
# using wxPython's # wx.html.HtmlWindow(parent, id, pos, size, style, name) # to show colourful text using relatively simple html code import wx import wx.html class MyFrame(wx.Frame): def __init__(self, parent, mytitle, mysize, html_code1, html_code2): wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle, size=mysize) # create an html label/window htmlwin1 = wx.html.HtmlWindow(self, wx.ID_ANY) htmlwin1.SetPage(html_code1) htmlwin2 = wx.html.HtmlWindow(self, wx.ID_ANY) htmlwin2.SetPage(html_code2) # position the labels with a box sizer sizer_v = wx.BoxSizer(wx.VERTICAL) sizer_v.Add(htmlwin1, proportion=1, flag=wx.EXPAND) sizer_v.Add(htmlwin2, proportion=2, flag=wx.EXPAND) self.SetSizer(sizer_v) # simple HTML code ... # text between <B> and </B> is bold # <BR> inserts a line break (or new line) # text between <FONT color="blue"> and </FONT> is that color # add size to the FONT tag html_code1 = """\ This shows you how to display text in color <BR> <FONT color="blue">like blue text</FONT> <FONT color="red"> or red text</FONT> <B> or maybe just bold ...</B> <BR><BR> <FONT color="green" size=5> You are in extreme <FONT color="red" size=5>danger</FONT> standing so close! </FONT> """ # a few more HTML tags ... # use <BODY> tags to add a background colour # text between <H3> and </H3> is header size # etc. etc. just experiment with the <> tags html_code2 = """\ <BODY bgcolor="#FFE47E"> look at the new background colour ... <H3>large header size</H3><H2>larger header size</H2> <BR> <FONT size="+4">even larger font size</FONT> <BR><BR> <FONT color="red" size="+4">larger </FONT> <FONT color="green" size="+4">size </FONT> <FONT color="blue" size="+4">and </FONT> <FONT color="magenta" size="+4">color</FONT> <BR> </BODY> """ app = wx.App() mytitle = "wx.html.HtmlWindow for HTML formatted text" width = 450 height = 350 frame = MyFrame(None, mytitle, (width, height), html_code1, html_code2) frame.Show(True) frame.Center() app.MainLoop()
drink her pretty
If you want to delay a function call in wxPython it is best to use function wx.FutureCall(delaytime, function) ...
python Syntax (Toggle Plain Text)
# use wx.FutureCall() to call a function after a set time # allows present process to go on import wx class MyFrame(wx.Frame): def __init__(self, parent, mytitle): wx.Frame.__init__(self, parent, -1, mytitle, size=(450, 300)) self.SetBackgroundColour('red') # call change_colour() after 2000 milliseconds (2 seconds) wx.FutureCall(2000, self.change_colour) def change_colour(self): self.SetBackgroundColour('green') self.ClearBackground() app = wx.App(0) MyFrame(None, 'change colour to green after 2 seconds').Show() app.MainLoop()
May 'the Google' be with you!
This little program will browse for a sound file and then play it when you click the play button ...
python Syntax (Toggle Plain Text)
# exploring wxPython's # wx.Sound(fileName, isResource=False) and # wx.lib.filebrowsebutton.FileBrowseButton(parent,labelText,fileMask) # vega import wx import wx.lib.filebrowsebutton class MyFrame(wx.Frame): def __init__(self, parent, mytitle): wx.Frame.__init__(self, parent, -1, mytitle, size=(600, 90)) self.SetBackgroundColour("green") panel = wx.Panel(self) self.fbb = wx.lib.filebrowsebutton.FileBrowseButton(panel, labelText="Browse for a WAVE file:", fileMask="*.wav") play_button = wx.Button(panel, -1, " Play ") self.Bind(wx.EVT_BUTTON, self.onPlay, play_button) # setup the layout with sizers # line up widgets horizontally hsizer = wx.BoxSizer(wx.HORIZONTAL) hsizer.Add(self.fbb, 1, wx.ALIGN_CENTER_VERTICAL) hsizer.Add(play_button, 0, wx.ALIGN_CENTER_VERTICAL) # create a border space border = wx.BoxSizer(wx.VERTICAL) border.Add(hsizer, 0, flag=wx.ALL|wx.EXPAND, border=10) # only neeed to set this sizer since it contains the hsizer panel.SetSizer(border) def onPlay(self, event): """the play button has been clicked""" filename = self.fbb.GetValue() sound = wx.Sound(filename) sound.Play(wx.SOUND_ASYNC) app = wx.App(0) mytitle = "wx.lib.filebrowsebutton.FileBrowseButton and wx.Sound" MyFrame(None, mytitle).Show() app.MainLoop()
May 'the Google' be with you!
Let's get a little playful and display a text in rainbow colors:
python Syntax (Toggle Plain Text)
# use wxPython's wx.richtext.RichTextCtrl # to create rainbow colored text import wx import wx.richtext class MyFrame(wx.Frame): def __init__(self, parent, title, rainbow_text): wx.Frame.__init__(self, parent, -1, title, size=(560, 220)) self.SetBackgroundColour("white") self.rich = wx.richtext.RichTextCtrl(self, -1, value="") self.rich.BeginFontSize(40) for c, colour in rainbow_text: self.rich.BeginTextColour(colour) self.rich.WriteText(c) self.rich.EndTextColour() rainbow = ['red', 'coral', 'yellow', 'green', 'blue'] text = """ Welcome to fabulous Las Vegas""" # create a list of (char, colour) tuples rainbow_text = [] ix = 0 for c in text: if c.isalpha(): colour = rainbow[ix] if ix < len(rainbow)-1: ix += 1 else: ix = 0 else: colour = 'white' rainbow_text.append((c, colour)) app = wx.App(0) title = 'Rainbow Text' MyFrame(None, title, rainbow_text).Show() app.MainLoop()
Last edited by Ene Uran; May 24th, 2009 at 7:14 pm.
drink her pretty
![]() |
Similar Threads
- Starting Python (Python)
- Autoupdater in wxPython (Python)
- what is python (Python)
Other Threads in the Python Forum
- Previous Thread: Easiest way to load images in wxPython?
- Next Thread: Python GUI Programming - Display images
| Thread Tools | Search this Thread |
address aliased anydbm app beginner bits calling casino changecolor cipher clear conversion coordinates corners count cturtle curves definedlines development dictionary dynamic events excel external feet file float format function generator getvalue handling homework iframe images import input ip java keycontrol line linux list lists loan loop maintain matching maze millimeter mouse number numbers output parsing path port prime programming py2exe pygame pymailer python queue random rational raw_input recursion recursive scrolledtext searchingfile signal singleton slicenotation split string strings tails text threading time tlapse tooltip tuple tutorial type ubuntu unicode url urllib urllib2 valueerror variable variables vigenere web whileloop word wxpython xlwt






