Pregunta

acabo de salir con mi forma novato de poner fin a un hilo, pero yo no sé por qué no está funcionando. ¿Podría alguien me ayuda por favor hacia fuera?

Aquí está mi código de ejemplo:

import wx
import thread
import time
import threading

class TestFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, parent = None, id = -1, title = "Testing", pos=(350, 110), size=(490, 200), style=wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.MINIMIZE_BOX)
        self.panel = wx.Panel(self)

        self.stop = False

        self.StartButton = wx.Button(parent = self.panel, id = -1, label = "Start", pos = (110, 17), size = (50, 20))
        self.MultiLine = wx.TextCtrl(parent = self.panel, id = -1, pos = (38, 70), size = (410, 90), style = wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_AUTO_URL)


        self.Bind(wx.EVT_BUTTON, self.OnStart, self.StartButton)
        self.Bind(wx.EVT_CLOSE, self.OnClose)

    def OnStart(self, event):
        self.StartButton.Disable()
        self.NewThread = threading.Thread(target = self.LongRunning)
        self.NewThread.start()

    def OnClose(self, event):
        self.stop = True
        BusyBox = wx.BusyInfo("Just a moment please!", self)
        wx.Yield()

        while True:
            try:
                if not self.NewThread.isAlive():
                    self.Destroy()
                    break
                time.sleep(0.5)
            except:
                self.Destroy()
                break

    def LongRunning(self):
        Counter = 1

        while True:
            time.sleep(2)
            print "Hello, ", Counter
            self.MultiLine.AppendText("hello, " + str(Counter) + "\n") #If you comment out this line, everything works fine. Why can't I update the fame after I hit the close button?
            Counter = Counter + 1
            if self.stop:
                break

class TestApp(wx.App):
    def OnInit(self):
        self.TestFrame = TestFrame()
        self.TestFrame.Show()
        self.SetTopWindow(self.TestFrame)
        return True

def main():
    App = TestApp(redirect = False)
    App.MainLoop()

if __name__ == "__main__":
    main()

Como se puede ver en mi código, hay un bucle infinito en el hilo, lo que le digo a la rosca que hacer es romper el bucle, una vez que haga clic en el botón Cerrar. Pero el problema es que cada vez que cuando pulso el botón de cierre, parece que el código pegado en la línea self.MultiLine.AppendText("hello, " + str(Counter) + "\n"), no sé por qué. Cualquiera puede ayudar?

¿Fue útil?

Solución

Trate de usar un método seguro de rosca, como wx.CallAfter al actualizar su información de varias líneas.

 def LongRunning(self):
     Counter = 1

     while True:
         time.sleep(2)
         print "Hello, ", Counter

         wx.CallAfter(self.updateMultiLine, "hello, " + str(Counter) + "\n")
         Counter = Counter + 1
         if self.stop:
             break

 def updateMultiLine(self, data):
     self.MultiLine.AppendText(data)

Otros consejos

En general con herramientas GUI, sólo un hilo debe acceder a las funciones de GUI. Una excepción es wx.CallAfter

A medida que (debe) conocer, defectos de software se pueden clasificar en tres grupos:

  1. Sus errores.
  2. Sus errores.
  3. Temas.

;)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top