By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,171 Members | 776 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,171 IT Pros & Developers. It's quick & easy.

trouble with wxPython intro

P: n/a
I'm trying to learn WxPython with the tutorial:
http://wiki.wxpython.org/Getting_Started

But I can't seem to get the example for events to work. I pasted the
code they had directly into an interpreter and it got a dialog to
appear and the program was able to close itself, but my own copy won't
work. As far as I can see, it isn't at all different except for the
name of the main frame, and so I differ to you for help.

my own code:
#!/usr/bin/python

import wx

ID_ABOUT=101
ID_EXIT=110

class MainWindow(wx.Frame):
def __init__(self,parent,id,title):
wx.Frame.__init__(self,parent,id,title,size=(200,1 00))
self.control = wx.TextCtrl(self,1,style=wx.TE_MULTILINE)
self.CreateStatusBar() #adds a status bar to the bottom
#Menu setup
filemenu = wx.Menu()
filemenu.Append(wx.ID_ABOUT,"&About","Info about the program")
filemenu.AppendSeparator()
filemenu.Append(wx.ID_EXIT,"E&xit","Terminate the program.")
#Menubar setup
menuBar = wx.MenuBar()
menuBar.Append(filemenu,"&File") #add file to the menubar
self.SetMenuBar(menuBar) #add in the menubar
# Add events
wx.EVT_MENU(self,ID_ABOUT,self.OnAbout)

wx.EVT_MENU(self,ID_EXIT,self.OnExit)

# Go!
self.Show(True)
def OnAbout(self,e):
print "Show!"
d = wx.MessageDialog(self,"A sample editor.","About Sample
Ed",wx.OK)
d.ShowModal()
d.Destroy()
def OnExit(self,e):
print "close!"
self.Close(True)

app = wx.PySimpleApp()

frame = MainWindow(None,wx.ID_ANY,'Small Ed!')

app.MainLoop()

May 31 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Daniel Gee wrote:
I'm trying to learn WxPython with the tutorial:
http://wiki.wxpython.org/Getting_Started

But I can't seem to get the example for events to work. I pasted the
code they had directly into an interpreter and it got a dialog to
appear and the program was able to close itself, but my own copy won't
work. As far as I can see, it isn't at all different except for the
name of the main frame, and so I differ to you for help.
--- code snipped ----
#Menu setup
filemenu = wx.Menu()
filemenu.Append(wx.ID_ABOUT,"&About","Info about the program")
Hi,

make it the following instead.

filemenu.Append(ID_ABOUT,"&About","Info about the program")

filemenu.AppendSeparator()
filemenu.Append(wx.ID_EXIT,"E&xit","Terminate the program.")
And again make it the following.

filemenu.Append(ID_EXIT,"E&xit","Terminate the program.")

--- code snipped ----
--
Kind Regards,
Anthony Irwin

http://www.irwinresources.com
http://www.makehomebusiness.com
email: anthony at above domains, - www.
May 31 '07 #2

P: n/a
On May 30, 11:41 pm, Anthony Irwin <nos...@noemailhere.nowherewrote:
Daniel Gee wrote:
I'm trying to learn WxPython with the tutorial:
http://wiki.wxpython.org/Getting_Started
I'm a wxPython beginner too, but instead of Anthony Irwin's
suggestions, I think you should delete these two lines:

ID_ABOUT=101
ID_EXIT=110

and use:

wx.ID_ABOUT
wx.ID_EXIT

throughout the body of your program. As far as I can tell, there is
no reason for you to be manually setting your own id's (is there
ever?). Instead, you can use the ids in the wx module.

In addition, I suggest you never use wx.PySimpleApp(). If you create
your own app class, you can send the error messages to the console.
Instead of always seeing a window that flashes at you briefly and
being left with no clue what went wrong, you can at least see an error
message and a line number in the console. Here's how you would do
that:

-----------------
import wx

class MyFrame(wx.Frame):
def __init__(self, mytitle):
wx.Frame.__init__(self, None, title= mytitle)

class MyApp(wx.App):
def __init__(self):
wx.App.__init__(self, redirect=False)

app = MyApp()

window = MyFrame("Testing")
window.Show()

app.MainLoop()

------------------

By setting redirect=False in wx.App.__init__(), the errors will be
sent to the console.

May 31 '07 #3

P: n/a
That's so simple I'm embarrassed. I should have noticed the change
from the example before to this one.

It works now, thank you.

May 31 '07 #4

P: n/a
On May 31, 1:56 am, 7stud <bbxx789_0...@yahoo.comwrote:
By setting redirect=False in wx.App.__init__(), the errors will be
sent to the console.
Hmmm...I just read a note I scribbled in the margin of my book that
says setting redirect=False sends the error messages to the console on
Mac and Windows. Are you using one of those operating systems? If
not, what os are you using, and do you get errors messages in the
console when using wx.PySimpleApp()?

Thanks
May 31 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.