Need help about 'good practice' and give event in bind widget

Hi i’ve finish my first python app using WxPython,
I’m trying to separate thing like graphical of each page in a file with the bind of those widget in same file.
ive got 3graphicall lib for each page + one that make everysizer / grid and the last lib creating the main frame).
Ive got problem to get the ‘event’ of button now that i don’t call them like self.ButtonOnClick now i’m doing lambda event: ButtonOnClick(self) and can’t find how to pass the event.
I’m also asking about how much do i need to separate def/class to respect ‘good practice’ ? (like if i was working in a company)
Thanks in advance for the help ! Don’t hesitate to ask thing that i should add, my english is not that good and i’m not get used to post on forums.

I’m giving an example of my code because after done a lot it’s still not work for example i have:

class GUI(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: GUI.__init__
        self.PathList = []
        self.PathSelected = []
        self.pastePath = ""
        self.listOfDisk = os.popen('ls /media/'+ os.getlogin()+"/").read()
        self.ActiveSSH= ''
        self.Password= ""


        self.Mp = InitMainPage(self)

        self.Pp = PremierePage(self)

        self.Sp = SecondePage(self)

        self.Tp = TroisiemePage(self)

        self.Sizers = SizeOfGridBagSizer(self)

That’s the main script in the page3 i have :

def TroisiemePage(self):
    self.SSHactive = wx.ToggleButton(self.OptionPannel,label="Activate SSH - Give the sudo psw\nfrom the ssh computer bellow",size=(200,11))
    self.PageThreeSizer.Add(self.SSHactive, (2,4), (0, 1), wx.EXPAND, 0)

In the Init script i have :

def initScript(self):
    self.BrowserSearch = BrowserSearch
    self.AddPaste = AddPaste
    self.FreeSpace = FreeSpace
    self.OnSelect = OnSelect
    self.AddToList = AddToList
    self.Copying = Copying
    self.UsedStorage = UsedStorage
    #Deuxieme Page
    self.refreshEVTerrors = refreshEVTerrors
    self.saveEVT = saveEVT
    self.refreshEVT = refreshEVT
    self.check_list_param =check_list_param
    self.NewPassword = NewPassword

And in one of the script file i’m doing :

def OnToggle(self,evt):
    if self.SSHactive.GetValue()==True and self.ActiveSSH != '':
        self.SSHactive.SetLabel('Desactivate SSH')
        os.system("echo "+self.Password +"| sshfs -o password_stdin  "+ self.ActiveSSH+":/home/ /mnt/SSH")
        self.SSHactive.SetLabel("Activate SSH - Give the sudo psw\nfrom the ssh computer bellow")
        if(os.popen("sudo ls /mnt/SSH").read()!=''):
            os.system("sudo fusermount -u /mnt/SSH/")

But like that i can’t get the event it does not exist anymore, so i can’t get the event.GetInt() anymore D: , how do i do ? Also i have been correctly sepparating them ? (for good practice for company work i mean, this is for my first intership and i want it to be clean)
Its just sample there is much more, and also all the import are good that’s why i didnt put them.


Not sure not functionning part is realy related to how the files, classes and sub classes are arranged in files. But any way I will try to answer with a simple approche.

Firstly, separate the design of the graphical parts from the event handlying function. I mean, init a classe (your frame) with all WX windows arrangement.

The event may be declarred but ampty.

Then make a super class based on this one and deal with the event in init.

The advantages:
1 you can first test the design and view independtly from the event.
2 event are still based on the windows classes and can access to it’s widget.

For exemple:

class MainWindows ( wx.Frame ):

    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"Administration du contrôle de temps", pos = wx.DefaultPosition, size = wx.Size( 800,600 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )

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

        bSizer1 = wx.BoxSizer( wx.VERTICAL )

        self.m_notebook1 = wx.Notebook( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.NB_LEFT )

        bSizer1.Add( self.m_notebook1, 1, wx.EXPAND |wx.ALL, 5 )

        self.SetSizer( bSizer1 )
        self.m_menubar1 = wx.MenuBar( 0 )
        self.MenuConfig = wx.Menu()
        self.menuAbout = wx.MenuItem( self.MenuConfig, wx.ID_ANY, u"A propos", u" Programme de supervision et modification des données du suivi de temps", wx.ITEM_NORMAL )
        self.MenuConfig.Append( self.menuAbout )

        self.menuConfig = wx.MenuItem( self.MenuConfig, wx.ID_ANY, u"Mon équipe", u"Définir votre équipe", wx.ITEM_NORMAL )
        self.MenuConfig.Append( self.menuConfig )

[ .... ]
        self.Centre( wx.BOTH )

        # Connect Events
        self.Bind( wx.EVT_MENU, self.OnAbout, id = self.menuAbout.GetId() )
        self.Bind( wx.EVT_MENU, self.OnConfigEquipe, id = self.menuConfig.GetId() )
    def __del__( self ):

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

This code is in a depend file

After that I created an other file for this main windows and handling event contening:

# Implementing MainWindows
class wx_AdminMainWindows( wxp.MainWindows ):
    def __init__( self, parent ):
        wxp.MainWindows.__init__( self, parent )
        self.parent = parent
    # Handlers for MainWindows events.
    def OnAbout( self, event ):
        # Texte à afficher
        # TODO: à remplacer par un text riche externe
        txt = """ La description de mon application ... ;)
             Par Alexis MARTINI

        myabout = ada.wx_DialAbout(self.parent)


Finaly you can access some information from the event by “event” variable bind in the function. Have a look to the doc on event handlying.
Expect it can answer to what you need. Quit new here too, not sure that we can speak in French :wink:

Bon courage!

Je suis français sa pourrais te paraitre plus clair, l’application n’a aucun problème graphique et les boutons fonctionne juste je nai plus leurs code d’event avant self.mafonction avait automatiquement self et évent mais ce n’est plus le cas depuis que je dois les séparer dans d’autres fichier et dappeler la fonction en lui donnant le self.
Im testing what youve given and telling you after if it resolve all!

Ive placed all the bind in the main class gui and ive still the problem 'need one more argument ‘event’ ’
J’ai placer dans ma class qui contient la frame et j’ai toujours le meme problème l’appel des def ne donne pas l’argument ‘event’

It’s difficult to understand what the actual question is. Maybe attach two .py files demonstrating the problem.

Anyway, you should think about a suitable architecture. wx events are not there to tie together components of a complex application.
Unfortunately, I’m not aware of a good introduction into architecture of complex GUI applications.
You should use one message dispatcher to dispatch application specific messages.
One available dispatcher is wx.lib.pubsub. Maybe this helps you: wxPython 4 and PubSub - Mouse Vs Python

For many simple use cases you can roll your own dispatcher.

E.g. I have an MP3 player application with messages (domain, message_type, data) and the code looks like this:

class NavigationWindow:
   def __init__(...):

    def on_play_button(self, event):"PC", "play")

    def on_folder_click(self, event):
        ..."MC", "folder", clicked_folder)

class Player:
   def __init__(...):
         ..., "PC")

    def receive_message(self, domain, message_type, data=None):
         if message_type=="play":

class AlbumMode:
    def receive_message(self, domain, message_type, data=None):
        if domain==self.domain and message_type=="folder":
            self.folder = data
  "PC", "play")  # start playback in new folder

class Application:
    def register_message_handler(self, handler, domain=None, message_type=None):


I don’t know what i can add xD
I’ve splited my project into multiple script because my formator told me it’s how you do it in ‘team company work’.

So the widget and the script attached to those are all in different file and now i can’t gave them the event of the widget in question, so for a list box the list box don’t have the event anymore so i can’t know wich one is check when checking it.

WxPython give them automaticaly the ‘self’ and the ‘event’ when i was just doing Bind(Event_Check (or something like that) , self.theFunctionToLoad) and now i do self.script = script so it have the self but if don’t have the event that the problem now.

I’ve also tried to do Bind(Event_Check,myfunc(self) ) but i can’t add the event because it didn’t exist ! :c

Well, Python is very flexible indeed (many don’t even like that much): you’ll have to import all those bits & pieces your ‘formator’ is keen on (there is even an importlib for dynamic loading) and then dot (’.’) them sort of all together (I hope you won’t run out of names): I can imagine you’ll end up in refactoring paradise :zzz:

I know about import i hope i just dont miss information because of my bad english but wxpython dont gave function the event if i call them from import.
The application is working fine if my whole application is in one file.
But after separating it much to be more readable i have this problem

Also i’m not doting them .importName because ive done ‘from /script/path import*’

best is you upload a skeleton example of your two modules (the more skeleton the better)

What is a squeleton ?

I don’t get it, you want something working for exemple ? I can share you the whole app, all is working while you don’t click the widgets

so let’s chair then, me not clicking & app be working: I’ll try me best :thought_balloon:


Ca ne me pose pas de souci pour continuer en Français. (sorry for English perosn passing here …) A voir si on se fait blammer! :wink:
As tu résolu ton souci?

Non pas résolut, j’ai vu en cour de python hier ce qu’était les super je pense que je n’ai pas réussi a le faire pour mon projet , comme je débute dans le codage j’ai du mal avec certains principe