473,503 Members | 2,046 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

wxpython: can't even create a Panel

I'm using the sample code of the file 'simple.py' and trying to make a
single window with a panel in it, but I keep getting an error. Here's my
code: (I know I might need something else, like a Show() method for the
panel, but the error stops on the first panel line anyway. I've tried a
Layout() method but it didn't get that far).

import wx
class MyFrame(wx.Frame):

def __init__(self, parent, title):
wx.Frame.__init__(self, parent, -1, title)

panel = wx.Panel(self)

class MyApp(wx.App):

def OnInit(self):
frame = MyFrame(None, 'My Test Frame')
frame.Show(True)
return True
app = MyApp()
app.MainLoop()

-----------------------
And the error:

Traceback (most recent call last):
File "C:\Python24\myscripts\wx_tests\wxtest.py", line 4, in -toplevel-
class MyFrame(wx.Frame):
File "C:\Python24\myscripts\wx_tests\wxtest.py", line 9, in MyFrame
panel = wx.Panel(self)
NameError: name 'self' is not defined


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

And if it helps, here is the code I'm using as a guide. It has the same
panel line, but obviously something about my code is different for the
'self' reference not to work:

#----------------------------------------------------------------------
# A very simple wxPython example. Just a wx.Frame, wx.Panel,
# wx.StaticText, wx.Button, and a wx.BoxSizer, but it shows the basic
# structure of any wxPython application.
#----------------------------------------------------------------------

import wx
class MyFrame(wx.Frame):
"""
This is MyFrame. It just shows a few controls on a wxPanel,
and has a simple menu.
"""
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, -1, title,
pos=(150, 150), size=(350, 200))

# Create the menubar
menuBar = wx.MenuBar()

# and a menu
menu = wx.Menu()

# add an item to the menu, using \tKeyName automatically
# creates an accelerator, the third param is some help text
# that will show up in the statusbar
menu.Append(wx.ID_EXIT, "E&xit\tAlt-X", "Exit this simple sample")

# bind the menu event to an event handler
self.Bind(wx.EVT_MENU, self.OnTimeToClose, id=wx.ID_EXIT)

# and put the menu on the menubar
menuBar.Append(menu, "&File")
self.SetMenuBar(menuBar)

self.CreateStatusBar()
# Now create the Panel to put the other controls on.
panel = wx.Panel(self)

# and a few controls
text = wx.StaticText(panel, -1, "Hello World!")
text.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD))
text.SetSize(text.GetBestSize())
btn = wx.Button(panel, -1, "Close")
funbtn = wx.Button(panel, -1, "Just for fun...")

# bind the button events to handlers
self.Bind(wx.EVT_BUTTON, self.OnTimeToClose, btn)
self.Bind(wx.EVT_BUTTON, self.OnFunButton, funbtn)

# Use a sizer to layout the controls, stacked vertically and with
# a 10 pixel border around each
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(text, 0, wx.ALL, 10)
sizer.Add(btn, 0, wx.ALL, 10)
sizer.Add(funbtn, 0, wx.ALL, 10)
panel.SetSizer(sizer)
panel.Layout()
def OnTimeToClose(self, evt):
"""Event handler for the button click."""
print "See ya later!"
self.Close()

def OnFunButton(self, evt):
"""Event handler for the button click."""
print "Having fun yet?"
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, "Simple wxPython App")
self.SetTopWindow(frame)

print "Print statements go to this stdout window by default."

frame.Show(True)
return True

app = MyApp(redirect=True)
app.MainLoop()
Jun 8 '06 #1
3 3202
John Salerno wrote:
I'm using the sample code of the file 'simple.py' and trying to make a
single window with a panel in it, but I keep getting an error. Here's my
code: (I know I might need something else, like a Show() method for the
panel, but the error stops on the first panel line anyway. I've tried a
Layout() method but it didn't get that far).

import wx

class MyFrame(wx.Frame):

def __init__(self, parent, title):
wx.Frame.__init__(self, parent, -1, title)

panel = wx.Panel(self)


hint: in python, indentation matters.

</F>

Jun 8 '06 #2
> def __init__(self, parent, title):
wx.Frame.__init__(self, parent, -1, title)

panel = wx.Panel(self)

It looks like a subtle difference between

panel = wx.Panel(self)

and

panel = wx.Panel(self)

As the error message states, there is no "self" at the
class-definition level...only within the method (__init__ in this
case).

In the example code from which you're working, note the
indentation level of the "panel = ..." line relative to the "def
__init__" line, and compare with your code.

Subtle, but important. :)

-tkc

Jun 8 '06 #3
Tim Chase wrote:
It looks like a subtle difference between

panel = wx.Panel(self)

and

panel = wx.Panel(self)


Ack! Thanks guys! What's funny is that at first I even had it beneath
the __init__ method, but then I unindented because it looked wrong for
some reason! :P

Well, now I can create a totally useless empty window, which I consider
a major accomplishment!
Jun 8 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3607
by: Tom Plunket | last post by:
(Is this an appropriate place to ask wxPython questions?) Here's some code (line breaks put in without testing them, I'm kinda a newbie hope they're ok): <busted.py> from wxPython.wx import...
1
2139
by: mdk.R | last post by:
Hello all: i'am installed wxPython 2.5 and Python2.3.4..i try execute script with wxPython but it show error: Traceback (most recent call last): File "E:\py\test.py", line 7, in ? import wx...
4
6570
by: KvS | last post by:
Hi all, I'm pretty new to (wx)Python so plz. don't shoot me if I've missed something obvious ;). I have a panel inside a frame, on which a Button and a StaticText is placed: self.panel =...
9
5510
by: zxo102 | last post by:
Hi everyone, I am using a python socket server to collect data from a socket client and then control a image location ( wxpython) with the data, i.e. moving the image around in the wxpython frame....
9
4401
by: Tyler | last post by:
Hello All: I am currently working on a project to create an FEM model for school. I was thinking about using wxPython to gather the 12 input variables from the user, then, after pressing the...
0
1926
by: Soren | last post by:
Hi, I'm trying to create a small GUI program where I can do plots using Matplotlib. I've been trying to borrow code from the examples at the matplotlib website, but I can't get it to work. I...
4
1915
by: Jimmy | last post by:
Hi, wxPython is cool and easy to use, But I ran into a problem recently when I try to write a GUI. The thing is I want to periodically update the content of StatixText object, so after create...
4
2838
by: Jimmy | last post by:
hi, all I'm having a problem with creating custom events in wxpython. I have a class A handling some data processing work and another class B of GUI matter. I need GUI to display information...
16
2366
by: Andrea Gavana | last post by:
Hi Diez & All, Do you mind explaining "why" you find it *buttugly*? I am asking just out of curiosity, obviously. I am so biased towards wxPython that I won't make any comment on this thread...
0
7204
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5586
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5018
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4680
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3171
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3162
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
741
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
391
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.