Python: how to add text inside a canvas?

前端 未结 2 1324
猫巷女王i
猫巷女王i 2020-12-10 13:35

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

相关标签:
2条回答
  • 2020-12-10 13:37

    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.")
    
    0 讨论(0)
  • 2020-12-10 14:00
    1. Use fg instead of fill argument
    2. Your font value is invalid. Read more about it.
    3. Remember about brackets after canvas.update
    0 讨论(0)
提交回复
热议问题