Python:如何在画布内添加文本? [英] Python: how to add text inside a canvas?

查看:193
本文介绍了Python:如何在画布内添加文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾多次尝试将文本添加到我的画布,但它只能通过单击按钮或在我的画布外部添加它.或者它会弹出一个单独的框.使用下面的代码-

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?

推荐答案

首先,第一段代码不起作用,因为您没有名为 canvas 的变量.但是,您有一个名为 self.canvas 的文件.当我在代码的第一位使用 self.canvas 并将其添加到工作程序中时,文本显示得很好.

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.

此外,在代码的第一部分中,您要执行 canvas.update.这绝对是零影响,因为您没有尾随括号.如果你修复它,它会起作用,但它真的没用.一旦进入事件循环,文本就会显示出来.

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.")

这篇关于Python:如何在画布内添加文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆