0

I wrote this 2 pieces of code:

  1. Create a class Myframe that inherits from wx.frame and create a App and it works fine as expected.

code1.py

import wx
class MyApp(wx.App):
    def OnInit(self):
        self.frame = MyFrame(None,title="MyFrame")
        self.SetTopWindow(self.frame)
        self.frame.Show()
        return True

class MyFrame(wx.Frame):
    def __init__(self,*args,**kwargs):
        super(MyFrame,self).__init__(*args,**kwargs)

        self.CreateStatusBar()
        self.SetStatusText("Initializing")
        self.CreateToolBar()

if __name__ == "__main__":
    app = MyApp(False)
    app.MainLoop()
  1. Now, I tried create a re-write the same directly calling the wx.App

code2.py

import wx

app = wx.App()
frame = wx.Frame(None,-1,"Test")
SetTopWindow(frame)
frame.show()

app.MainLoop()

Couple of things are not working as code-1.

  1. The window closes immediately.
  2. The SetTopWindow is not working..It says undeclared variable...thats correct..but How do I refer SetTopWindow ?

1 Answer 1

2

The window closes immediately because the code is buggy. As you already know, SetTopWindow is not defined, but there's another issue too. Frame doesn't have a show() method. It has a Show() method. Notice the difference in capitalization. If you look at the first code example, you'll see that you are calling the app object's SetTopWindow() method. You just need to do that here too:

import wx

app = wx.App()
frame = wx.Frame(None,-1,"Test")
app.SetTopWindow(frame)
frame.Show()

app.MainLoop()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.