views:

27

answers:

1

in wxpython, is there a event that can occur when one of the function of the panel or frame is called? thanks; for example, self.Bind(wx.EVT_Onfunctioncalled, self.OnDoSomething)

Well, I have to changed this question to a more specific one as follows:

I mean, well, the following process: I click on the menu "file" on the main window (as most GUI application looks), then "open", then a file directory dialog pops up, then I select a file, then I click "okay" on that dialog, then, I want that this file's name would appear on the listctrl on this main window, displaying which file I had select

+1  A: 

Here's a runnable example that does what you want.

import wx

class ListTest(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, -1, title, size=(380, 230))          
        panel = wx.Panel(self, -1)

        #Create a list                
        self.list = wx.ListCtrl(panel, -1, style=wx.LC_REPORT) 
        self.list.InsertColumn(0, 'File-path', width=140)

        #Do the layout
        hbox = wx.BoxSizer(wx.HORIZONTAL)
        hbox.Add(self.list, 1, wx.EXPAND)
        panel.SetSizer(hbox)
        self.Centre()
        self.Show(True)

        #Create Menu
        menubar = wx.MenuBar()
        file = wx.Menu()
        self.loadFile = wx.MenuItem(file, -1, '&Open\tCtrl+L', 'Open a file')
        file.AppendItem(self.loadFile)
        menubar.Append(file, '&File')
        self.SetMenuBar(menubar)

        self.Bind(wx.EVT_MENU, self.on_openFile)


    def on_openFile(self, evt):
        loadFileDlg = wx.FileDialog(
            self, message="Open File",
            defaultDir="",
            defaultFile="",
            style=wx.OPEN | wx.CHANGE_DIR
            )
        #If the user clicked the open file button
        if loadFileDlg.ShowModal() == wx.ID_OK:          
            #Get the file path
            path = loadFileDlg.GetPath()
        loadFileDlg.Destroy()

        #Call your file analysis method or whatever here

        #Create a message dialog 
        fileAnalysedDlg = wx.MessageDialog(self, "File Has Been Analysed", "File Has Been Analysed", wx.OK)   
        fileAnalysedDlg.ShowModal()
        fileAnalysedDlg.Destroy()

        #Add filepath to list
        self.list.InsertStringItem(0, path)


app = wx.App()
ListTest(None, 'list test')
app.MainLoop()
volting
@volting: thank you, is it possible to use this code like this: open the file for a analysis, when the analysis is done, a dialog pops up, then I click okay on that dialog, and then, the filename is showed into this listctrl??
serina
@Serina: Sure why not? you just need to add code for a message dialog and your code to analyse the file of course. What do you not know how to do?
volting
@volting: Okay, I am not sure how to connect the event when I click the okay on the dialog to the display of filenames inside the listctrl, because these seem to be differnet things
serina
Unlike things, such as key press, on enter window, etc, that can be directly binded to the listctrl panel or frame, when I click the okay on a message dialog, how could I expect the listctrl respond this? please, thanks
serina
in particular, when these two parts are in different classes, i mean, the listctrl is in one class, whereas the anaylsis process is done in another class, so is the message dialog that tells me the process ends. then I click on the dialog, the file names display on the listctrl that is not of this class, well, I was confused...
serina
Ok Iv updated my answer to include a `messageDialog`, execution is sequential so the dialog will only open after the file has been analysed. If file analysis takes a long time then you will need to take extra measures to prevent your GUI from freezing.
volting
@volting: thank you so much! but what about the display of the filename was triggered in another class? it should not be binded to the panel as in your code, it happend when I click okay on the message dialog from another class
serina
maybe I could send my codes to you then, is that alright?? please , thanks !
serina
@Serina: I don't fully understand your problem. Why don't you try whatever it is you want to do, and if you then have difficulties ask a new question regarding this, provide a clear description of the problem and an example where possible, then myself or someone else will be able to help you better. Thanks
volting