2014-12-20 4 views
1

며칠 전에 파이썬을 시작했는데 애니메이션을 보았습니다. 나는 "Bounce!"라고 불리는 것에 대해 작업하고 있으며, 다른 설정과 함께 그 이름을 화면에 가지고 있습니다. 설정은 FPS 및 속도 (증분)입니다. 그러나 타원형이 튀어 오르는 것처럼 이러한 설정이 변경되고 텍스트도 함께 변경되기를 원하지만 어떻게해야할지 모르겠습니다. 이것은 현재 코드입니다 :파이썬 tkinter 모듈 텍스트 전환

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를 사용하고 있습니다.

+1

Tkinter를이 방법을 사용하도록 설계되지 않았습니다. 이 사이트의 tkinter에서 애니메이션을 만드는 것에 대한 몇 가지 질문과 답변이 있으며, 모두 mainloop을 실행하고 주기적으로 기능을 실행하기 위해 'after'를 호출하는 것을 중심으로 진행됩니다. –

답변

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) 
+2

문제를 해결하는 방법에 대해 최소한의 설명을 제공해야하므로 문자와 문자를 전체 코드와 비교할 필요가 없습니다. –

2

itemconfigure을 사용하여 캔버스의 요소 설정을 변경할 수 있습니다 (here 참조). 또한 루프 변수 x이 텍스트의 ID를 할당 한 변수를 섀도 잉합니다. 일반적으로 당신이 GUI에서`sleep`를 호출해서는 안, 나 자신의 무한 루프가 -

text = canvas.create_text(...) 
    ... 
    canvas.itemconfigure(text, text=('FPS: %s; Speed: %s'% (fps,increment)))