Reputation: 327
Im making a "math bubble" game in python 3.3, its where ovals(bubbles) move around randomly on the canvas, These bubbles have a number on them e.g.2 and the user must pop/click the bubbles in relation to the question e.g. Multiples of 2, user will click bubbles that say 2,4,6,8 etc.
Trouble is, I dont know how or if its possible to put a number on an oval Ive created. Please help me >.<
Code so far:
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()
Upvotes: 2
Views: 4980
Reputation:
To put text in a tkinter canvas, use the create_text method. To place it on an oval you make, set the position to be the center of the oval. In this case, the center of each oval you make is (xval+15, yval+15). See below:
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()
Each oval you now make will have "mytext" written in it.
However, you might want to look into animation software for more complex applications involving movement. Some good examples are Pygame and Livewires. But that is up to you.
Upvotes: 5