sword
sword

Reputation: 225

How can I update the textctrl content in GUI?

I am trying to write my first wxpython GUI program,in my program,I want to get anothor software window's title,if the title change,clear the old title and show the new title in GUI,I test in cmd,it can get the title in a loop,but I don't konw how to set a event in GUI to update the title.

my code:

def getinfos():
    tempWindowName=win32gui.GetWindowText (find_window())
    while True:
        titles=[]
        if (tempWindowName==win32gui.GetWindowText (find_window())):
            pass
        else:
            tempWindowName=win32gui.GetWindowText (find_window())               
            titles.append(tempWindowName)

            return title[0]

    time.sleep(1000)  

and the GUI code:

import controller2
import time

########################################################################
class InfoPanel(wx.Panel):

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)

        try:
            self.titleResults = controller2.getinfos()                            
        except:               
            self.titleResults = 'no data'


        mainSizer = wx.BoxSizer(wx.VERTICAL)

        self.titlecontent = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE|wx.TE_RICH|wx.TE_LEFT|wx.TE_WORDWRAP|wx.NO_BORDER)                                                                                                                    
        self.titlecontent.SetBackgroundColour('white')
        self.settitle()

        mainSizer.Add(self.yejicontent, 2.5, wx.ALL|wx.EXPAND, 5)

        self.SetSizer(mainSizer)


    #----------------------------------------------------------------------
    def settitle(self):


        self.titlecontent.SetValue("%s"%self.titleResults)



########################################################################
class InfoFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="title",size=(500, 880))
        panel = InfoPanel(self)
        style= self.GetWindowStyle()
        self.SetWindowStyle(style|wx.STAY_ON_TOP)


class MyApp(wx.App):
    def OnInit(self):
        self.infoFrame=InfoFrame()        
        self.SetTopWindow(self.infoFrame)
        self.infoFrame.Show(True)        
        return True

#----------------------------------------------------------------------
if __name__ == "__main__":
    app = MyApp(False)
    app.MainLoop()

Thanks for your time and appreciate for any advise.

Upvotes: 2

Views: 856

Answers (2)

Mike Driscoll
Mike Driscoll

Reputation: 33071

Put the getinfos function/method into a thread. When the title changes, have the thread use wx.CallAfter or wx.PostEvent (both of which are thread-safe) to tell the GUI to update. If you don't put it into a thread, you're GUI will be very unresponsive.

Pubsub rocks, but won't work in this case if you're running that getinfos function in your wxPython main loop as it will block it. You could use pubsub in the thread in combination with those threadsafe methods I mentioned though.

Upvotes: 1

GP89
GP89

Reputation: 6730

You can send a custom wx event or setup pubsub.

Upvotes: 0

Related Questions