dot.Py
dot.Py

Reputation: 5157

How am I supposed to use wxFormBuilder Python GUI code in my applications?

i'm trying to create a GUI for my Python program in Windows, and i'm kinda confused atm..

i've created the GUI using wxFormBuilder and it looks like this:

# -*- coding: utf-8 -*- 

###########################################################################
## Python code generated with wxFormBuilder (version Jun 17 2015)
## http://www.wxformbuilder.org/
##
## PLEASE DO "NOT" EDIT THIS FILE!
###########################################################################

import wx
import wx.xrc

###########################################################################
## Class MainFrame
###########################################################################

class MainFrame ( wx.Frame ):

    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"Editor_SPED_LP", pos = wx.DefaultPosition, size = wx.Size( 320,255 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )

        self.SetSizeHintsSz( wx.Size( 320,255 ), wx.Size( 320,255 ) )

        bSizer1 = wx.BoxSizer( wx.VERTICAL )

        self.m_textCtrl2 = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer1.Add( self.m_textCtrl2, 0, wx.ALL|wx.EXPAND, 5 )

        self.m_btn_abrirArq = wx.Button( self, wx.ID_ANY, u"Abrir arquivo...", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer1.Add( self.m_btn_abrirArq, 0, wx.ALL, 5 )

        self.m_btn_editarTxt = wx.Button( self, wx.ID_ANY, u"Editar .txt", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer1.Add( self.m_btn_editarTxt, 0, wx.ALL, 5 )

        self.m_gauge1 = wx.Gauge( self, wx.ID_ANY, 100, wx.DefaultPosition, wx.DefaultSize, wx.GA_HORIZONTAL )
        self.m_gauge1.SetValue( 0 ) 
        bSizer1.Add( self.m_gauge1, 0, wx.ALL, 5 )

        self.m_btn_ajuda = wx.Button( self, wx.ID_ANY, u"Ajuda", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer1.Add( self.m_btn_ajuda, 0, wx.ALL, 5 )

        self.m_staticText2 = wx.StaticText( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText2.Wrap( -1 )
        bSizer1.Add( self.m_staticText2, 0, wx.ALL, 5 )

        self.m_btn_sair = wx.Button( self, wx.ID_ANY, u"Sair", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer1.Add( self.m_btn_sair, 0, wx.ALL, 5 )


        self.SetSizer( bSizer1 )
        self.Layout()

        self.Centre( wx.BOTH )

        # Connect Events
        self.m_btn_abrirArq.Bind( wx.EVT_BUTTON, self.abrirArquivo )
        self.m_btn_editarTxt.Bind( wx.EVT_BUTTON, self.editarTxt )
        self.m_btn_ajuda.Bind( wx.EVT_BUTTON, self.janelaAjuda )
        self.m_btn_sair.Bind( wx.EVT_BUTTON, self.sair )

    def __del__( self ):
        pass


    # Virtual event handlers, overide them in your derived class
    def abrirArquivo( self, event ):
        event.Skip()

    def editarTxt( self, event ):
        event.Skip()

    def janelaAjuda( self, event ):
        event.Skip()

    def sair( self, event ):
        event.Skip()

when i run it from the terminal it doesnt show any errors...

how am i supposed to use it with my application?

i've found this tutorial here, but i couldn't understood it..

also i tried to add that code at the end of the script:

 if __name__ == "__main__":
        app = wx.App(False)
        frame = MainFrame()
        frame.Show()
        app.MainLoop()

and got this error:

File "gui.py", line 80, in <module>
    frame = MainFrame(seld,parent)
NameError: name 'self' is not defined

so.. it looks like the gui code is fine, the problem is that i dont know how to initialize it and add some functions..

can someone help me with that, please?

Upvotes: 0

Views: 8224

Answers (3)

Umar Yusuf
Umar Yusuf

Reputation: 984

The issue you are having is with the "init" function/method. It accepts two arguments (self & parent) which you have to specify when creating the MainFrame object.

def __init__( self, parent ):
     ......

self is specified automatically (you don't explicitly specify it)

parent is set to None (you explicitly specify it as None)

See the screen shot below and a wxFormBuilder - Tutorial on “A GUI for making GUIs” for Python

enter image description here

Upvotes: 4

Mike Driscoll
Mike Driscoll

Reputation: 33071

You just need to add the following to the bottom of your program:

if __name__ == '__main__':
    app = wx.App(False)
    frame = MainFrame(parent=None)
    frame.Show()
    app.MainLoop()

What this tells Python to do is to create an instance of the wx.App class. The False parameter tells wxPython that it shouldn't redirect stdout to a new window. Next we create an instance of your class, MainFrame and set its parent to None. After that, we Show the frame. If we don't, the code will still work, but you won't see anything. Finally we call the application object's MainLoop function so that wxPython can actually start running and processing events.

Here's the full code:

# -*- coding: utf-8 -*-

###########################################################################
## Python code generated with wxFormBuilder (version Jun 17 2015)
## http://www.wxformbuilder.org/
##
## PLEASE DO "NOT" EDIT THIS FILE!
###########################################################################

import wx
import wx.xrc

###########################################################################
## Class MainFrame
###########################################################################

class MainFrame ( wx.Frame ):

    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"Editor_SPED_LP", pos = wx.DefaultPosition, size = wx.Size( 320,255 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )

        self.SetSizeHintsSz( wx.Size( 320,255 ), wx.Size( 320,255 ) )

        bSizer1 = wx.BoxSizer( wx.VERTICAL )

        self.m_textCtrl2 = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer1.Add( self.m_textCtrl2, 0, wx.ALL|wx.EXPAND, 5 )

        self.m_btn_abrirArq = wx.Button( self, wx.ID_ANY, u"Abrir arquivo...", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer1.Add( self.m_btn_abrirArq, 0, wx.ALL, 5 )

        self.m_btn_editarTxt = wx.Button( self, wx.ID_ANY, u"Editar .txt", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer1.Add( self.m_btn_editarTxt, 0, wx.ALL, 5 )

        self.m_gauge1 = wx.Gauge( self, wx.ID_ANY, 100, wx.DefaultPosition, wx.DefaultSize, wx.GA_HORIZONTAL )
        self.m_gauge1.SetValue( 0 )
        bSizer1.Add( self.m_gauge1, 0, wx.ALL, 5 )

        self.m_btn_ajuda = wx.Button( self, wx.ID_ANY, u"Ajuda", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer1.Add( self.m_btn_ajuda, 0, wx.ALL, 5 )

        self.m_staticText2 = wx.StaticText( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText2.Wrap( -1 )
        bSizer1.Add( self.m_staticText2, 0, wx.ALL, 5 )

        self.m_btn_sair = wx.Button( self, wx.ID_ANY, u"Sair", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer1.Add( self.m_btn_sair, 0, wx.ALL, 5 )


        self.SetSizer( bSizer1 )
        self.Layout()

        self.Centre( wx.BOTH )

        # Connect Events
        self.m_btn_abrirArq.Bind( wx.EVT_BUTTON, self.abrirArquivo )
        self.m_btn_editarTxt.Bind( wx.EVT_BUTTON, self.editarTxt )
        self.m_btn_ajuda.Bind( wx.EVT_BUTTON, self.janelaAjuda )
        self.m_btn_sair.Bind( wx.EVT_BUTTON, self.sair )

    def __del__( self ):
        pass


    # Virtual event handlers, overide them in your derived class
    def abrirArquivo( self, event ):
        event.Skip()

    def editarTxt( self, event ):
        event.Skip()

    def janelaAjuda( self, event ):
        event.Skip()

    def sair( self, event ):
        event.Skip()

if __name__ == '__main__':
    app = wx.App(False)
    frame = MainFrame(parent=None)
    frame.Show()
    app.MainLoop()

Upvotes: 4

dot.Py
dot.Py

Reputation: 5157

i got a sucessfull attempt using:

#importing wx files
import wx
#import the newly created GUI file
import gui


#inherit from the MainFrame created in wxFormBuilder and create janelaPrincipal
class janelaPrincipal(gui.MainFrame):
    #constructor
    def __init__(self,parent):
        #initialize parent class
        gui.MainFrame.__init__(self,parent)


    #what to do when each function button is clicked

    def editarTxt(self,event):
        try:
            #write the editarTxt code inside here

        except Exception:
            print 'error'


#mandatory in wx
#create an app, False stands for not deteriction stdin/stdout
#refer manual for details
app = wx.App(False)

#create an object of janelaPrincipal
frame = janelaPrincipal(None)
#show the frame
frame.Show(True)
#start the applications
app.MainLoop()

but i actually dont know why it worked.. still need some explanation, please.. :-)

Upvotes: 0

Related Questions