user1235711
user1235711

Reputation: 19

Inheritance, Python and wxWidget

I am trying to use Python to create GUI. I have downloaded wxPython and made a "hello world" window. However, I have found that my code is a little different other code examples. In particular, that when the other examples want to create a GUI they inherit from the wx library whereas I don't, so what's the difference between my class and other class?

My source code:

import wx
class Window ():
    def init (self, parent, id, windowname):
        mywindow = wx.Frame (parent, id, windowname)
        mywindow.Show(True)
        return True 

app = wx.App(False)
wind = Window()
wind.init(None, wx.ID_ANY, "windowname")
t.oninit("Hello World" , "watch")
app.MainLoop()

Sample source code from the book:

import wx
class App(wx.App):
    def OnInit(self):
        frame = wx.Frame(parent=None, title='Bare')
        frame.Show()
        return True

app = App()
app.MainLoop()

Upvotes: 1

Views: 320

Answers (3)

Mike Driscoll
Mike Driscoll

Reputation: 33111

This is the correct way to do it:

import wx
class Window(wx.Frame):
    def __init__ (self, parent, id, windowname):
        mywindow = wx.Frame (parent, id, windowname)
        mywindow.Show(True)


app = wx.App(False)
wind = Window(None, -1, "windowname")
app.MainLoop()

As the others have said, naming the class instance "Window" means nothing. You have to subclass from wx.Frame to do it right. In fact, I wouldn't call it "Window" because there's actually a higher level widget called "wx.Window" that might cause you confusion later.

You should go through the zetcode wxPython tutorial: http://zetcode.com/wxpython/ and maybe check out some of the examples in the wxPython demo, wiki or my blog: http://www.blog.pythonlibrary.org/

You should also add a wx.Panel instance as the only child of the frame to make it look right on all systems and to enable tabbing between widgets.

Upvotes: 2

Hugh Bothwell
Hugh Bothwell

Reputation: 56714

Calling it a Window doesn't make it one!

By inheriting from wx classes, you get all their pre-existing functionality - in the example, the App class inherits all of the wx.App class's structure and behavior, ie it already knows how to do lots of things (like how to start up, how to process input events, how to shut down when told to, etc).

Your from-scratch class doesn't inherit anything - it has no logic other than what you have defined, which isn't enough to make it actually "act like" a window.

Upvotes: 1

Blender
Blender

Reputation: 298562

The wx.App class has a method called OnInit() that is called when the application has been initialized. The second code chunk overrides that method with custom code, namely code that displays a frame.

Your code doesn't do that, which is probably why you're having problems.

Upvotes: 1

Related Questions