Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: how to add text inside a canvas?

I have tried many times to add text to my canvas but it only adds it with a click of a button or on the outside of my canvas. Or it pops up in a separate box. Using the code below-

def text():
    canvas.create_text(100,10,fill="darkblue",font="Times 20 italic bold",text="Click the bubbles that are multiples of two.")
    canvas.update

It never worked. So my question is how do I add text in my canvas to start of my game?

like image 988
Pokemon_SL Avatar asked Jul 19 '13 01:07

Pokemon_SL


People also ask

How do I show text in canvas python?

You can display one or more lines of text on a canvas C by creating a text object: id = C . create_text( x , y , option , ...) The text color to be used when the text is active, that is, when the mouse is over it.

How do I create text in canvas?

To draw text on a canvas, the most important property and methods are: font - defines the font properties for the text. fillText(text,x,y) - draws "filled" text on the canvas. strokeText(text,x,y) - draws text on the canvas (no fill)


1 Answers

For one, the first snippet of code doesn't work because you don't have a variable named canvas. You have one called self.canvas, however. And when I use self.canvas in the first bit of code and add it to the working program, the text shows up just fine.

Also, in that first bit of code you do canvas.update. That has absolutely zero effect because you don't have the trailing parenthesis. If you fix that it will work, but it's really useless. The text will show up as soon as the event loop is entered.

All you need to do is add one line right after you create the canvas:

self.canvas = Canvas(root, width=800, height=650, bg = '#afeeee')
self.canvas.create_text(100,10,fill="darkblue",font="Times 20 italic bold",
                        text="Click the bubbles that are multiples of two.")
like image 162
Bryan Oakley Avatar answered Sep 18 '22 13:09

Bryan Oakley