Hi everyone,

I recently installed Slackware 13/x86_64 on one of my boxes and now I'm having troubles with Python code that uses threading and Tkinter. This problem never existed on other Linux distro's, it just appeared with Slackware.

The box in question
Acer Athlon x86_64 single core used to run Mandriva/64 and the Python code worked fine.

Now it runs Slackware13/64 and Python threading and Tkinter are not getting along at all(program crashes with out of stack space infinite loop for the thread).

Has anyone experienced anything like this? Any suggestion?...Gerard4143

TKinter isn't very thread safe. What exactly are you trying to do? If you want to have more than one windows open Tkinter has a solution for that, look up TopLevel for tkinter(I'm not gonna post an example unless i know your problem).

The program I tested at first was a simple client/server program where the server used Tkinter as its graphical front end...this failed. I then tried a very simple threading program and again it failed...here's the code for the simple program..Note the processor is single core

#! /usr/bin/python

import Tkinter, thread, time

class MyLab:
	DATA = 'this is the marquee message! '
	START = 'start marquee'
	STOP = 'stop marquee'
	def __init__(self, master):
		self.looping = 1
		self.frame = Tkinter.Frame(master)
		self.mylabel = Tkinter.Label(self.frame, text = MyLab.DATA, background = 'white')	
		self.start = Tkinter.Button(self.frame, text = MyLab.START, command = self.startit)	
		
		self.mylabel.pack()
		self.start.pack()
		self.frame.pack()
		
	def startit(self):
		if (self.start['text'] == MyLab.START):
			self.looping = 1
			self.start['text'] = MyLab.STOP
			thread.start_new_thread(self.marquee, ())
		else:
			self.looping = 0
			self.start['text'] = MyLab.START	
	
	def marquee(self):
		while self.looping:
			self.text = self.mylabel['text'][-1] + self.mylabel['text'][:-1]
			self.mylabel['text'] = self.text	
			time.sleep(.2)	
		
root = Tkinter.Tk()

mymar = MyLab(root)

root.mainloop()

Note all the code that I've mentioned above worked well on the same box but with Mandriva 64 bit installed on it

I'm not in the position to debug it right now, since im at school, but I'll try it out later. Multithreading in python is only able to use one core since its only running in one python interperter, however the library multiprocess will spawn another python interpreter thus granting you the ability to run on multiple cores at once.

Is this your error "RuntimeError: main thread is not in main loop"?
I'd really recommend making another class for marquee and threading that.

Luckily for you I have nothing to do right now, Sooo I made the appropriate changes, tell me how this works:

#! /usr/bin/python

import Tkinter, threading, time
mylabel=None
global mylabel
class marquee(threading.Thread):
	def run(self):
		while self.looping:
			self.text = mylabel['text'][-1] + mylabel['text'][:-1]
			mylabel['text'] = self.text	
			time.sleep(.2)
class MyLab:
	DATA = 'this is the marquee message! '
	START = 'start marquee'
	STOP = 'stop marquee'
	def __init__(self, master):
		self.looping = 1
		self.frame = Tkinter.Frame(master)
		mylabel = Tkinter.Label(self.frame, text = MyLab.DATA, background = 'white')	
		self.start = Tkinter.Button(self.frame, text = MyLab.START, command = self.startit)	
		
		mylabel.pack()
		self.start.pack()
		self.frame.pack()
		
	def startit(self):
		if (self.start['text'] == MyLab.START):
			self.looping = 1
			self.start['text'] = MyLab.STOP
			tr=marquee()
		else:
			self.looping = 0
			self.start['text'] = MyLab.START
#I moved this function to another class which is threaded.
#	def marquee(self):
#		while self.looping:
#			self.text = mylabel['text'][-1] + mylabel['text'][:-1]
#			mylabel['text'] = self.text	
#			time.sleep(.2)	
		
root = Tkinter.Tk()

mymar = MyLab(root)

root.mainloop()

This solved the Runtime error i got before, it should fix your problem.

Luckily for you I have nothing to do right now, Sooo I made the appropriate changes, tell me how this works:

#! /usr/bin/python

import Tkinter, threading, time
mylabel=None
global mylabel
class marquee(threading.Thread):
	def run(self):
		while self.looping:
			self.text = mylabel['text'][-1] + mylabel['text'][:-1]
			mylabel['text'] = self.text	
			time.sleep(.2)
class MyLab:
	DATA = 'this is the marquee message! '
	START = 'start marquee'
	STOP = 'stop marquee'
	def __init__(self, master):
		self.looping = 1
		self.frame = Tkinter.Frame(master)
		mylabel = Tkinter.Label(self.frame, text = MyLab.DATA, background = 'white')	
		self.start = Tkinter.Button(self.frame, text = MyLab.START, command = self.startit)	
		
		mylabel.pack()
		self.start.pack()
		self.frame.pack()
		
	def startit(self):
		if (self.start['text'] == MyLab.START):
			self.looping = 1
			self.start['text'] = MyLab.STOP
			tr=marquee()
		else:
			self.looping = 0
			self.start['text'] = MyLab.START
#I moved this function to another class which is threaded.
#	def marquee(self):
#		while self.looping:
#			self.text = mylabel['text'][-1] + mylabel['text'][:-1]
#			mylabel['text'] = self.text	
#			time.sleep(.2)	
		
root = Tkinter.Tk()

mymar = MyLab(root)

root.mainloop()

This solved the Runtime error i got before, it should fix your problem.

The thread doesn't run at all...The question really isn't how to get this running as to why it works on mandriva64 and not slackware 64...I thought Python was platform independent

The only possible way i see this working it different python versions and/or Tkinter versions, or possibly thread module verions, btw I forgot to kick it off with the start function not to mention countless bugs, i'm going to fix if for the sake of doing it if you want me to post it just say something.

They were both using Python 2.6.2

Well when I ran your script I got a runtime error for something about threads. Are you getting any errors? Is it giving anything, any feedback?

This article has been dead for over six months. Start a new discussion instead.