Threads freeze

Post here if you need help with creating a Graphical User Interface in Python.

Threads freeze

Postby irtomek » Sat May 25, 2013 11:10 am

Hey I've got some issues with creating a plane of colorful fields. This is they freeze for some time after a moment of use.

What is more I find it difficult to terminate Threads after closing an application.

Code: Select all

import sys
import threading
import time
import random
from PyQt4 import QtGui,QtCore
from multiprocessing import Process

class Field(QtGui.QWidget, threading.Thread):
   def __init__(self, x, y, X, Y, time, table, p):
      self.x = x
      self.y = y
      self.X = X
      self.Y = Y
      self.time = time
      self.p = p

      self.table = table

   def directChange(self):
      self.r = random.randint(0,255)
      self.g = random.randint(0,255)
      self.b = random.randint(0,255)

      p = self.palette()
      p.setColor(self.backgroundRole(), QtGui.QColor(self.r,self.g,self.b))

   def northRow(self):
      if 0<self.x:
         return self.x-1
      else: return self.X-1
   def southRow(self):
      if self.x < self.X-1:
         return self.x+1
      else: return 0
   def westCol(self):
      if 0 < self.y:
         return self.y-1
         return self.Y-1
   def eastCol(self):
      if self.y < self.Y-1:
         return self.y+1
      else: return 0

   def indirectChange(self, x, y, table):
      self.r = (table[self.northRow()][y].r + table[self.southRow()][y].r + \
            table[x][self.westCol()].r + table[x][self.eastCol()].r) /4

      self.g = (table[self.northRow()][y].g + table[self.southRow()][y].g + \
            table[x][self.westCol()].g + table[x][self.eastCol()].g) /4

      self.b = (table[self.northRow()][y].b + table[self.southRow()][y].b + \
            table[x][self.westCol()].b + table[x][self.eastCol()].b) /4

      p = self.palette()
      p.setColor(self.backgroundRole(), QtGui.QColor(self.r,self.g,self.b))

   def run(self):
         with threading.Lock():
   =  threading.Timer(self.time,
               self.indirectChange(self.x, self.y, self.table)
      finally: threadLimiter.release()

class DanceFloor(QtGui.QWidget):
   def __init__(self, i, j, time, p):
      self.i = i
      self.j = j

      self.pos = [[0 for k in range(j)] for l in range(i)]
      for k in range(i):
         for l in range(j):
            self.pos[k][l] = Field(k,l, i, j, time, self.pos, p)

      grid = QtGui.QGridLayout()
      for k in range(i):
         for l in range(j):
      for k in range(i):
         for l in range(j):
            th = self.pos[k][l]

   def closeEvent(self, event):

app = QtGui.QApplication(sys.argv)
threadLimiter = threading.BoundedSemaphore(15)
frame = DanceFloor(7,7, 0.1, 0.1)


if __name__ == '__main__':
Posts: 2
Joined: Sat May 25, 2013 11:06 am

Re: Threads freeze

Postby irtomek » Sat May 25, 2013 11:22 am

But I found it is possible to UNFREEZE threads with resizing a window !
Posts: 2
Joined: Sat May 25, 2013 11:06 am

Return to GUI

Who is online

Users browsing this forum: Yoriz and 1 guest