[Tutor] Fwd: Re: trouble placing code into wxpython

Alan Gauld alan.gauld at btinternet.com
Thu Feb 2 09:54:01 CET 2012


On 02/02/12 06:56, shane wrote:

> i was trying to put the math.py into a frame Ive watched many tutorials.

That sounds like you've been using video tutorials? Those are a good 
start but you are probably better using a written one and working 
through them doing the examples. You can try my GUI topic as a quick 
start. It uses Tkinter but finishes with a wxPython version too so it 
might be enough to get you started. The concepts are similar.

> But cant seem to find out how to or where to place the code
> the files I want combined are attached.

#
import wx
app = wx.PySimpleApp()
frame = wx.Frame(None,-1,'hello')
frame.Show(1)
app.MainLoop()
#

This code creates the GUI(*) but does nothing.
You need to define the event handling functions and
bind them to the widgets/events. In this case you
don't have much to bind things to...

A Frame is just a container, it doesn't do anything as such.
You need to add other widgets such as text labels, entries,
buttons etc to the Frame. Then attach your functions
to the widgets. Any wxPython tutorial will show you
how to do that.

(*) wxPython is very OOP oriented, you normally have to create your own 
sub-class of Frame. There is not much you can do with a standard Frame 
object... If you are not comfortable with creating subclasses then you 
should study OOP a bit more before attempting wxPython. Either that or 
use Tkinter instead which is more amenable to procedural style code.

The other thing is your math code is full of print statements. They 
won't work in a GUI. You need to get your code to return strings and 
then display those strings in the GUI.

Finally, rewrite your fin() method to be non-recursive. Use a while 
loop. Otherwise you are likely to get into all sorts of trouble, its one 
of the most common beginners mistakes but recursion is not a good 
looping technique in Python. However, in a GUI you won't need fin() 
because the GUI mainloop will take over that function.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list