Python Forum

Full Version: canvas.create_text doesn't work in a function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I've been trying to write a reaction time task with stimuli appearing on screen using the tkinter module.

While this function does work:
def show_stim(stim, side, verpos, police):
	x = screensize[0]/2 + side
	y = screensize[1]/2 + verpos
	PTcanvas.create_text(x, y, text=stim.value,	fill=stim.color, font=police)


PrimTask = Tk() #Fenetre de la tâche principale
PTcanvas = Canvas(PrimTask, background='black', height = screensize[1], width = screensize[0]) 
PTcanvas.pack()

#fonts definition
italic = font.Font(PrimTask, family='Times', size=50, slant='italic')
underli = font.Font(PrimTask, family='Times', size=50, underline=1)
polices=[italic, underli]
#stimulus instanciation and display
x = stimulus('lettre')
show_stim(x, 150, 150, italic)

PrimTask.mainloop()
PrimTask.quit()
Using show_stim() in a function does not work:
def Trial():
	pair = ['lettre', 'chiffre']
	vert = [-150, 150]
	verposD = vert[randint(0,1)]
	verposG = vert[randint(0,1)]
	Stype = pair[randint(0,1)]
	StimD = stimulus(Stype)
	if Stype == 'lettre':
		StimG = stimulus('chiffre')
	else: StimG = stimulus('lettre')
	SDpolice = polices[randint(0,1)]
	SGpolice = polices[randint(0,1)]
	show_stim(StimD, 150, verposD, SDpolice)
	show_stim(StimG, -150, verposG, SGpolice)
Error:
Traceback (most recent call last): bricole.py", line 71, in <module> Trial() bricole.py", line 49, in Trial show_stim(StimD, 150, verposD, SDpolice) bricole.py", line 55, in show_stim PTcanvas.create_text(x, y, text=stim.value, fill=stim.color, font=police) File "C:\Users\Vincent\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 2498, in create_text return self._create('text', args, kw) File "C:\Users\Vincent\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 2474, in _create *(args + self._options(cnf, kw)))) _tkinter.TclError: invalid command name ".!canvas"
I don't know why but the error goes back in the _create method, and I can't see where it tries to to call this ".!canvas" command.
Does anybody have an idea?

(P.S.: I'm new using python, I'm sorry for my messy lines of code)
pass the canvas in the function.
def show_stim(canvas, stim, side, verpos, police):
    #etc
    canvas.create_text(x, y, text=stim.value, fill=stim.color, font=police)

or
example
import tkinter as tk
import tkinter.font as font

# global container
class PrimTask:
    pass

def show_stim(stim, side, verpos, police):
    x = PrimTask.screensize[0]/2 + side
    y = PrimTask.screensize[1]/2 + verpos
    PrimTask.canvas.create_text(x, y, text=stim, fill='blue', font=police)

def main():
    PrimTask.screensize = 800, 600
    PrimTask.root = tk.Tk()
    PrimTask.canvas = tk.Canvas(PrimTask.root, background='black',
        height = PrimTask.screensize[1], width = PrimTask.screensize[0])
    PrimTask.canvas.pack()

    #fonts definition
    italic = font.Font(PrimTask.root, family='Times', size=50, slant='italic')
    underline = font.Font(PrimTask.root, family='Times', size=50, underline=1)
    polices=[italic, underline]

    show_stim('lettre', 150, 150, italic)

    PrimTask.root.mainloop()

if __name__ == '__main__':
    main()
Thank you for your help. The first solution did work perfectly.