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
#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


Messages In This Thread
RE: canvas.create_text doesn't work in a function - by Windspar - Jan-16-2018, 04:23 PM

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 957 Nov-22-2023, 05:58 PM
Last Post: Axel_Erfurt
  Tkinter function to clear old canvas and start a fresh "operation" zazas321 5 10,034 Oct-01-2020, 04:16 AM
Last Post: zazas321
  [Tkinter] Scrollbar doesn't work on Canvas in Tkinter DeanAseraf1 3 9,612 Sep-19-2019, 03:26 PM
Last Post: joe_momma
  [Tkinter] Resizing image inside Canvas (with Canvas' resize) Gupi 2 25,325 Jun-04-2019, 05:05 AM
Last Post: Gupi
  [Kivy] Installed but doesn't work antonmichel 13 14,189 Jan-30-2018, 10:34 AM
Last Post: antonmichel
  Gtk justification doesn't work rebelxt 2 4,680 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