Frage

Dear members of our community,

The following is a simple wx.app with only a text control. When this text control is edited a large process starts. If one edits the control slowly, the editing feels normal. However, if the human is a fast typer, the editor stalls. I think the solution is to kill the previous call of the function self.VeryLargeProcess().

How do you do it? Is there a better way to do this?

import wx

class MyFrame(wx.Frame):
    def __init__(self):
         wx.Frame.__init__(self, None, wx.ID_ANY, "My Frame")
         main_panel = wx.Panel(self)

        static_box = wx.StaticBox( main_panel, -1, "Text control" )
        static_box.SetFont( wx.Font( 15, wx.ROMAN, wx.NORMAL, wx.NORMAL ) )
        main_sizer = wx.StaticBoxSizer( static_box, wx.VERTICAL )

        text_ctrl = wx.TextCtrl( main_panel, wx.ID_ANY, "Edit me, a large process will start", wx.DefaultPosition, [200,100], wx.TE_MULTILINE )
        main_sizer.Add( text_ctrl, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )

        main_panel.SetSizer( main_sizer )
        main_sizer.SetSizeHints( self )

        self.Bind(wx.EVT_TEXT, self.VeryLargeProcess, text_ctrl)

    def VeryLargeProcess(self, event):

        # kill previous self.VeryLargeProcess()   

        for i in range(0, 3000000):
            a = i

if __name__ == '__main__':
    app = wx.App()
    frame = MyFrame()
    frame.Show()
    app.MainLoop()
War es hilfreich?

Lösung

If the process takes a "long time" to run, then you need to put that process in a separate thread or your GUI's responsiveness will slow or stop. The wxPython wiki has a good article on the various ways to use threads and I wrote a tutorial on the topic as well.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top