trouble with wxPython intro

D

Daniel Gee

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,100))
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()
 
A

Anthony Irwin

Daniel said:
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.
 
7

7stud

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()
 
D

Daniel Gee

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.
 
7

7stud

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top