我几天前刚刚开始使用Python,我已经开始使用动画了。我正在开发一个名为“Bounce!”的屏幕,它在屏幕上有其他设置。设置为FPS和速度(增量)。但是,随着椭圆弹跳,这些设置会发生变化,我希望文本随之改变,但我不知道如何改变。这是我目前的代码:
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)
我使用的是Python 3.4.2。
答案 0 :(得分:2)
您可以使用itemconfigure
更改画布上元素的设置(请参阅here)。另请注意,循环变量x
正在为您指定文本ID的变量生成阴影。
text = canvas.create_text(...)
...
canvas.itemconfigure(text, text=('FPS: %s; Speed: %s'% (fps,increment)))
答案 1 :(得分:0)
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)