wxpython how to import button actions from another py file

0

Hello, my question is a strange thing, but if in wxpython I have created a window in the main.py file

import wx
import wx.xrc
#from accion import evento

class MyFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, id=wx.ID_ANY, title=u"Muestra", pos=wx.DefaultPosition, size=wx.Size(500, 300),
                          style=wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL)

        self.SetSizeHints(wx.DefaultSize, wx.DefaultSize)

        bSizer1 = wx.BoxSizer(wx.VERTICAL)

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

        self.buton = wx.Button(self, wx.ID_ANY, u"ejecutar", wx.DefaultPosition, wx.DefaultSize, 0)
        bSizer1.Add(self.buton, 0, wx.ALIGN_CENTER | wx.ALL, 5)

        self.SetSizer(bSizer1)
        self.Layout()

        self.Centre(wx.BOTH)

        # Connect Events
        self.buton.Bind(wx.EVT_BUTTON, self.iniciar)

    def __del__(self):
        pass

    # Virtual event handlers, overide them in your derived class
    def iniciar(self, event):
        #evento()    quiero ejecutar desde otro py
        event.Skip()


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

Now I create the file "accion.py" but I want to make that by clicking, it imports the commands of action.py, I think I need a class class or a def that is created in action, for example

def evento(self):
    texto = self.self.texto.GetLineText(0)
    print(texto)

and if I want to create for example more button actions or others, this is in order to write less code of the parent and. Can you help me please?

    
asked by royer 03.07.2018 в 04:26
source

1 answer

0

First of all, as you put it, what you really want is to implement a class along different modules, move an instance method from the class definition to an external module .

Python is not a language that imposes too many restrictions, it lets you do many things, some very good and some very bad, it is assumed that the programmer is sufficiently "older" to know what you should and should not do at any given time . It is indeed possible to do what you want in various ways, for example:

  • Nothing prevents you from creating a module where you define the functions (as mere functions, not as instance methods) or enclose them in a class and delegate them. Something similar is what you are trying, but you have two errors:

    • You must weigh the instance to the function as an argument so that you can access the attribute since it is not an instance method and does not receive it as the first argument ( self ) automatically.

    • In addition it is not self.self.texto but self.texto in any case, generically it is instancia.texto .

    # main.py
    
    from accion import evento
    
    def iniciar(self, event):
        evento(self)
        event.Skip()
    
    # accion.py
    
    def evento(inst):
        texto = inst.texto.GetLineText(0)
        print(texto)        
    
  • Another option is to create a class that contains the methods and derive from it, which is known as mixing :

    # main.py
    
    import wx
    import wx.xrc
    from accion import MyFrameMixin
    
    class MyFrame(wx.Frame, MyFrameMixin):
        def __init__(self, parent):
            wx.Frame.__init__(self, parent, id=wx.ID_ANY, title=u"Muestra", pos=wx.DefaultPosition, size=wx.Size(500, 300),
                              style=wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL)
    
            self.SetSizeHints(wx.DefaultSize, wx.DefaultSize)
    
            bSizer1 = wx.BoxSizer(wx.VERTICAL)
    
            self.texto = wx.TextCtrl(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0)
            bSizer1.Add(self.texto, 0, wx.ALIGN_CENTER | wx.ALL, 5)
    
            self.buton = wx.Button(self, wx.ID_ANY, u"ejecutar", wx.DefaultPosition, wx.DefaultSize, 0)
            bSizer1.Add(self.buton, 0, wx.ALIGN_CENTER | wx.ALL, 5)
    
            self.SetSizer(bSizer1)
            self.Layout()
    
            self.Centre(wx.BOTH)
    
            # Connect Events
            self.buton.Bind(wx.EVT_BUTTON, self.iniciar)
    
        def __del__(self):
            pass
    
        # Virtual event handlers, overide them in your derived class
        def iniciar(self, event):
            self.evento()
            event.Skip()
    
    
    if __name__ == "__main__":
        app = wx.App(False)
        frame = MyFrame(None)
        frame.Show()
        app.MainLoop()
    
    # accion.py
    
    class MyFrameMixin:
    
        def evento(self):
            texto = self.texto.GetLineText(0)
            print(texto)
    

However, you should ask yourself if these options are the most appropriate, readable and easy to maintain. The mixin is a very good and totally appropriate resource when you want to extend the functionality of a class, in this case it is not that the class MyFrameMixin add functionality to MyFrame , directly MyFrame does not work without it ...

If you want to delete code from your main.py you can simply take your class MyFrame to another module and in the main module or wherever you import the class and instantiate or inherit it if we want to extend its functionality.

    
answered by 03.07.2018 / 23:06
source