Reputation:
I just started Python a few days ago, and I have gotten to animations. I am working on one called "Bounce!", and it has that name on the screen, with other settings. The settings are FPS and Speed (increment). But, as an oval bounces, these settings change, and I want the text to change along with it, but I do not know how. This is my current code:
import time
from tkinter import *
tk = Tk()
fps=30
increment=2
canvas = Canvas(tk,height='500',width='500')
canvas.pack()
canvas.create_oval(175,100,325,250,fill='red',outline='black')
canvas.create_line(50,100,450,100)
canvas.create_line(50,450,450,450)
canvas.create_text(250,30,text='Bounce!',fill='red',font=('Consolas',30))
x = canvas.create_text(250,75,text=('FPS: %s; Speed: %s'% (fps,increment)),fill='black',font=('Consolas',20)) #mainproblem
while True:
increment += 1
if increment % 2 == 0 and fps > 1:
fps = fps - 1
#I want the 'x' text to change to the current FPS and Speed here, but I do not know how.
for x in range(0, int(200/increment)):
canvas.move(1,0,increment)
tk.update()
time.sleep(1/fps)
for x in range(0, int(200/increment)):
canvas.move(1,0,-(increment))
tk.update()
time.sleep(1/fps)
I am using Python 3.4.2.
Upvotes: 1
Views: 1051
Reputation: 373
import time
from tkinter import *
tk = Tk()
fps=30
increment=2
t = ('FPS: %s; Speed: %s'% (fps,increment))
canvas = Canvas(tk,height='500',width='500')
canvas.pack()
canvas.create_oval(175,100,325,250,fill='red',outline='black')
canvas.create_line(50,100,450,100)
canvas.create_line(50,450,450,450)
canvas.create_text(250,30,text='Bounce!',fill='red',font=('Consolas',30))
w = canvas.create_text(250,75,text= t,fill='black',font=('Consolas',20)) #mainproblem
while True:
increment += 1
if increment % 2 == 0 and fps > 1:
fps = fps - 1
t = ('FPS: %s; Speed: %s'% (fps,increment))
canvas.itemconfig(w, text= t)
for x in range(0, int(200/increment)):
canvas.move(1,0,increment)
tk.update()
time.sleep(1/fps)
for x in range(0, int(200/increment)):
canvas.move(1,0,-(increment))
tk.update()
time.sleep(1/fps)
Upvotes: 0
Reputation: 82899
You can use itemconfigure
to change the settings of elements on the canvas (see here). Also note that your loop variable x
is shadowing the variable you assigned the ID of the text to.
text = canvas.create_text(...)
...
canvas.itemconfigure(text, text=('FPS: %s; Speed: %s'% (fps,increment)))
Upvotes: 2