Python Forum
[Tkinter] canvas.create_text doesn't work in a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] canvas.create_text doesn't work in a function
#1
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)
Reply
#2
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()
99 percent of computer problems exists between chair and keyboard.
Reply
#3
Thank you for your help. The first solution did work perfectly.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] graphyview setdata() doesn't work after recieving a mount of data ugly_curry_garlic 3 669 Nov-22-2023, 05:58 PM
Last Post: Axel_Erfurt
  Tkinter function to clear old canvas and start a fresh "operation" zazas321 5 9,247 Oct-01-2020, 04:16 AM
Last Post: zazas321
  [Tkinter] Scrollbar doesn't work on Canvas in Tkinter DeanAseraf1 3 9,299 Sep-19-2019, 03:26 PM
Last Post: joe_momma
  [Tkinter] Resizing image inside Canvas (with Canvas' resize) Gupi 2 25,024 Jun-04-2019, 05:05 AM
Last Post: Gupi
  [Kivy] Installed but doesn't work antonmichel 13 13,628 Jan-30-2018, 10:34 AM
Last Post: antonmichel
  Gtk justification doesn't work rebelxt 2 4,481 Nov-05-2016, 01:52 PM
Last Post: rebelxt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020