Domanda

Ho appena uscito con il mio modo di porre fine niubbo un filo, ma io non so perché non funziona. Would qualcuno mi aiuto per favore fuori?

Ecco il mio codice di esempio:

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()

Come si può vedere nel mio codice, c'è un loop infinito nel filo, quello che dico il filo da fare è rompere fuori dal giro una volta che si fa clic sul pulsante di chiusura. Ma il problema è, ogni volta quando ho colpito il pulsante di chiusura, a quanto pare il codice attaccato alla linea self.MultiLine.AppendText("hello, " + str(Counter) + "\n"), non so perché. Chiunque può aiutare?

È stato utile?

Soluzione

Provare a usare un metodo thread-safe, come wx.CallAfter quando l'aggiornamento del multilinea.

 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)

Altri suggerimenti

In generale GUI toolkit, un solo filo deve accedere alle funzioni della GUI. Un'eccezione è wx.CallAfter

Come si (dovrebbe) conoscere, i difetti del software possono essere classificati in tre gruppi:

  1. I tuoi insetti.
  2. I loro errori.
  3. Le discussioni.

;)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top