Click here to Skip to main content
15,741,818 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I`m writing a program which asks for a string and code, and opens a new window and shows the string like a LED Text. for now, to test it, I have written it in a way that new window draws a sinus but it`s not shown. While running the code in __init__ function of LED class works well.

What I have tried:

from Tkinter import *
import time
from math import sin
WIDTH = 200
class App:
	def __init__(self, master):
		#Initialize Data Panel
		self.dataFrame = Frame(master)
		self.stringLabel = Label(self.dataFrame, text="Enter a string: ")
		self.stringBox = Text(self.dataFrame, height=1, width=30)
		self.codeLabel = Label(self.dataFrame, text="Enter display code: ")
		self.codeBox = Text(self.dataFrame, height=1, width=5)
		self.dataFrame.pack(padx=5 ,pady=10 , side=LEFT)
		#Initialize Button panel
		self.buttonFrame = Frame(master)
		self.button = Button(self.buttonFrame, text="Run",
		self.buttonFrame.pack(padx=5 ,pady=20 , side=RIGHT)

	def run(self):
		string = self.getString()
		code = self.getCode()
		root = Tk()
		led = LED(root)
	def getString(self):
		string = self.stringBox.get("1.0", "end-1c")
		return string
	def getCode(self):
		code = self.codeBox.get("1.0", "end-1c")
		return code
class LED:
	def __init__(self, master):
		WIDTH, HEIGHT = 200, 200
		canvas = Canvas(master, width=WIDTH, height=HEIGHT, bg="#000000")
		img=PhotoImage(master = canvas, width=WIDTH, height=HEIGHT)
		canvas.create_image((WIDTH/2, HEIGHT/2), image=img, state="normal")
		for x in range(4 * WIDTH):
			y = int(HEIGHT/2 + HEIGHT/4 * sin(x/80.0))
			img.put("#ffffff", (x//4,y))
root = Tk()
app = App(root)
Updated 21-Jun-16 1:12am
Richard MacCutchan 21-Jun-16 4:25am    
I have not used Tk with Python, but I notice you are calling root.mainloop a second time, from within the method. Is this correct?
Ali-RNT 21-Jun-16 4:36am    
No, I don`t think so, because the new window is created. And the second root is in App class and does not interfere with the root defined in main code.
Richard MacCutchan 21-Jun-16 4:46am    
So does that create a second window? I think I need to learn Tk for myself.
Richard MacCutchan 21-Jun-16 5:49am    
I have tried as many variations as I can think of in the above code, including calling LED directly, different values for x and y, but cannot get any pixels drawn on the image. However, when i tried a small sample code I found at it worked correctly.

1 solution

The problem was caused by the fact that the img object was being garbage collected after the LED.__init__ method completed, so the canvas appeared blank. Adding something like the extra line below holds it in place.
img=PhotoImage(master = canvas, width=WIDTH, height=HEIGHT)
canvas.create_image((WIDTH/2, HEIGHT/2), image=img, state="normal")
canvas.theimage = img # add attribute to prevent GC
Share this answer
Ali-RNT 21-Jun-16 7:41am    
Thanks for the time you have spent on this question. It worked!
Richard MacCutchan 21-Jun-16 8:36am    
It was an interesting challenge.

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