Search code examples
pythonfunctioninheritancebuttonwxpython

I've created a GUI using wxPython and the functions aren't executed when I run my app


I'm using python 2.7 and i'm creating a GUI for a specific text editor using wxPython.. I've created these 2 scripts to run my program: gui.py and app1.py and i've put both of them in a single folder.

The problem is that once i run app1.py, it opens the GUI perfectly.. But when i press the buttons, the commands that i've inserted into app1.py aren't executed.. not even in the terminal like a simple print ()..


I'm using Python "2.7.9"

I can run import wx

and my wx.__version__ is "3.0.2.0"


So, what's the problem with my code?

Is it a inheritance concept problem?

Should i use super() to fix that? If so, how am i supposed to use it?

The gui.py 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,231 ), style = wx.DEFAULT_FRAME_STYLE|wx.SYSTEM_MENU|wx.TAB_TRAVERSAL )

        self.SetSizeHintsSz( wx.Size( -1,-1 ), wx.Size( -1,-1 ) )

        bSizer1 = wx.BoxSizer( wx.VERTICAL )

        self.m_textBox = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, wx.TE_LEFT|wx.TE_READONLY )
        bSizer1.Add( self.m_textBox, 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_textoProgresso = wx.StaticText( self, wx.ID_ANY, u"Aguardando arquivo..", wx.Point( -1,-1 ), wx.DefaultSize, wx.ALIGN_CENTRE )
        self.m_textoProgresso.Wrap( -1 )
        bSizer1.Add( self.m_textoProgresso, 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.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 )

    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()

The app1.py code:

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

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

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

    #handlers for MyFrame_MainFrame events
    def abrirArquivo(self, event):
        try:
            #abrir fileDialog box
            openFileDialog = wx.FileDialog(self, "Open", "", "", "Text files (*.txt)|*.txt", wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
            openFileDialog.ShowModal()
            #salvar o caminho para o arquivo
            filepath = openFileDialog.GetPath()
            #alterar a textbox para "arquivo encontrado"

            TextCtrl.SetValue(self, "teste")
            StaticText.SetValue(self, u"Arquivo encontrado..")
        except Exception:
            print "erro_abrir_arquivo"

    def editarTxt(self, event):
        try:
            #editar texto
            dial = wx.MessageDialog(None, "TESTE", 'Ajuda - Editor SPED', wx.OK)
            dial.ShowModal()
            dial.Destroy()
        except Exception:
            print "erro_editar_txt"

    def janelaAjuda( self, event):
        try:
            #mostrar janela de ajuda
            dial = wx.MessageDialog(None, "'1 - Apertar o botao 'escolher arquivo' \n2 - Selecionar o .txt do SPED da L.P.\n3 - Aguarde o processamento..\n4 - O arquivo editado sera salvo na mesma pasta do arquivo selecionado\n5 - Fim'", 'Ajuda - Editor SPED', wx.OK)
            dial.ShowModal()
            dial.Destroy()
        except Exception:
            print "erro_jan_ajuda"

# run the app and render-it continuously
class MyApp(wx.App):
    def OnInit(self):
        self.frame = MyFrame(None)
        self.SetTopWindow(self.frame)
        self.frame.Show(True)
        print("wxApp criado com sucesso.")
        return True
if __name__ == "__main__":
    # do not redirect stdout to the gui
    app = MyApp(redirect=False)
    # render gui continuously
    app.MainLoop() 

Solution

  • For the logbook:

    As mentioned in my comment I copied both files in an empty folder and the buttons seem fully functional when I click them, I get text in the console, a file picker window, a messagebox.

    To begin checking with any installation issues of wx, check with

    import wx
    print wx.__version__
    

    if it returns a version number for your wxPython installation.

    I'm glad you could solve it copying in a plain new folder, maybe some import or filename confusion happened, as it seems to work now with the two files on their own.