python如何将文本添加到椭圆形?

Pok*_*_SL 2 python tkinter python-3.x

我在python 3.3中制作了一个“数学泡泡”游戏,椭圆(泡泡)在画布上随机移动,这些泡泡上有一个数字,例如eg2,用户必须弹出/单击与问题相关的泡泡,例如倍数。 2,用户将点击显示2、4、6、8等的气泡。

麻烦的是,我不知道如何或是否有可能在Ive创建的椭圆形上放置数字。请帮助我>。<

到目前为止的代码:

数学泡泡

from tkinter import *
import random


def quit():
    root.destroy()


def bubble():
    xval = random.randint(5,765)
    yval = random.randint(5,615)
    canvas.create_oval(xval,yval,xval+30,yval+30,   fill="#00ffff",outline="#00bfff",width=5)
    canvas.update()

def main():
    global root
    global tkinter
    global canvas
    root = Tk()
    root.title("Math Bubbles")
    Button(root, text="Quit", width=8, command=quit).pack()
    Button(root, text="Start", width=8, command=bubble).pack()
    canvas = Canvas(root, width=800, height=650, bg = '#afeeee')
    canvas.pack()
    root.mainloop()

main()
Run Code Online (Sandbox Code Playgroud)

iCo*_*dez 5

要将文本放在tkinter画布中,请使用create_text方法。要将其放置在您制作的椭圆上,请将位置设置为椭圆的中心。在这种情况下,您制作的每个椭圆的中心为(xval + 15,yval + 15)。见下文:

def bubble():
    xval = random.randint(5,765)
    yval = random.randint(5,615)
    canvas.create_oval(xval,yval,xval+30,yval+30, fill="#00ffff",outline="#00bfff",width=5)
    canvas.create_text(xval+15,yval+15,text="mytext")
    canvas.update()
Run Code Online (Sandbox Code Playgroud)

您现在制作的每个椭圆形都将写入“ mytext”。

但是,您可能希望针对涉及运动的更复杂应用程序研究动画软件。Pygame和Livewires是一些很好的例子。但这取决于您。