Click here to Skip to main content
16,004,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my python program using TKinter, the function "code04" calls printIt04 and that function prints something each time and waits for 0.2 seconds. but when "code04" called, the whole app freezes and then all of the text will be printed once. How can I solve it?

What I have tried:

Python
	def code04(self):
		w = len(self.Matrix[0])
		for x in range(0, 8):
			self.printIt04(w, x)

.
.
.
	def printIt04(self, w, x):
		if (w > 200):
			MStartCol = int(round( (w - 200)/2))
			MEndCol = 200 + MStartCol
			IStartCol = 0
			IEndCol = 200
		else:
			MStartCol = 0
			MEndCol = w
			IStartCol = int(round( (200 - w)/2))
			IEndCol = IStartCol + w
		r = int(round( (w - 200)/2))
		for col in range(MStartCol, MEndCol):
			for row in range(0, x+1):
				if self.Matrix[7-row][col] == 1:
					self.img.put ("#ff0000", ( IStartCol - MStartCol + col, 7-row))
				else:
					self.img.put ("#000000", ( IStartCol - MStartCol + col, 7-row))
		time.sleep(0.2)
Posted
Updated 26-Jun-16 7:23am
Comments
Sergey Alexandrovich Kryukov 26-Jun-16 16:41pm    
I cannot imagine a single valid case of using sleep in UI thread. Why?
—SA

1 solution

The reason it does this is because all of your code is running on the startup thread, otherwise known as the "UI thread". Your code is hogging the thread, stopping it from receiving and processing the WM_PAINT messages the thread is getting. Until your code returns and the thread goes back to idle, these paint messages will just queue up. When you finally give up control all the queued message get processed and the window gets repainted showing you the end result.

You either have to use a timer to "tick" through your loop or move all of the "work" to a background thread freeing up the UI thread to process the messages.
 
Share this answer
 
Comments
Ali-RNT 26-Jun-16 19:29pm    
Thank you! I think it's better to use a separate thread, although I haven't ever usedit in python. Do you know a good reference for that?
Dave Kreskowiak 26-Jun-16 21:13pm    
I don't use Python at all.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900