user2778477
user2778477

Reputation:

wxPython: program runs but not displaying

Before I made some changes to the following program, everything went fine:

Program before modification:

#! /usr/bin/env python
""" A bare-minimum wxPython program """

import wx

class MyApp(wx.App):
    def OnInit(self):
        return True

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

if __name__ == '__main__':
    app = wx.App()
    frame = MyFrame(None, "Sample")
    frame.Show(True)
    app.MainLoop()

But after I put frame into the definition of OnInit, the program runs without syntax error but nothing displayed.:(

Program after modification:

#! /usr/bin/env python
""" A bare-minimum wxPython program """

import wx

class MyApp(wx.App):
    def OnInit(self):
        self.frame = MyFrame(None, "Sample")    ## add two lines here
        self.frame.Show(True)
        return True

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

if __name__ == '__main__':
    app = wx.App() 
    app.MainLoop()

I try to use the debugger and step over the program. It seems that self.frame is not defined (not even appear from beginning to end).

What am I going wrong with the program? I'm very new to Python and wxPython, please help. Thx.

EDIT:

app = MyApp()

stdout/stderr:
NameError: global name 'Show' is not defined

Upvotes: 0

Views: 1124

Answers (1)

falsetru
falsetru

Reputation: 368904

You should create MyApp (not wx.App) object:

#! /usr/bin/env python
""" A bare-minimum wxPython program """

import wx

class MyApp(wx.App):
    def OnInit(self):
        self.frame = MyFrame(None, "Sample")    ## add two lines here
        self.frame.Show(True)
        return True

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

if __name__ == '__main__':
    app = MyApp() # <---
    app.MainLoop()

Upvotes: 1

Related Questions