Tkinter가 간단한 동작을 수행하려고 시도는 Y 이동을위한 X 이동 하나, 다른 : self.momentum
2 개 정수를 포함하고있는 배열이고Tkinter에서이 모양이 왜 천천히 업데이트됩니까?
import tkinter as tk
class GameApp(object):
"""
An object for the game window.
Attributes:
master: Main window tied to the application
canvas: The canvas of this window
"""
def __init__(self, master):
"""
Initialize the window and canvas of the game.
"""
self.master = master
self.master.title = "Game"
self.master.geometry('{}x{}'.format(500, 500))
self.canvas = tk.Canvas(self.master)
self.canvas.pack(side="top", fill="both", expand=True)
self.start_game()
#----------------------------------------------#
def start_game(self):
"""
Actual loading of the game.
"""
player = Player(self)
#----------------------------------------------#
#----------------------------------------------#
class Player(object):
"""
The player of the game.
Attributes:
color: color of sprite (string)
dimensions: dimensions of the sprite (array)
canvas: the canvas of this sprite (object)
window: the actual game window object (object)
momentum: how fast the object is moving (array)
"""
def __init__(self, window):
self.color = ""
self.dimensions = [225, 225, 275, 275]
self.window = window
self.properties()
#----------------------------------------------#
def properties(self):
"""
Establish the properties of the player.
"""
self.color = "blue"
self.momentum = [5, 0]
self.draw()
self.mom_calc()
#----------------------------------------------#
def draw(self):
"""
Draw the sprite.
"""
self.sprite = self.window.canvas.create_rectangle(*self.dimensions, fill=self.color, outline=self.color)
#----------------------------------------------#
def mom_calc(self):
"""
Calculate the actual momentum of the thing
"""
self.window.canvas.move(self.sprite, *self.momentum)
self.window.master.after(2, self.mom_calc)
#----------------------------------------------#
#----------------------------------------------#
root = tk.Tk()
game_window = GameApp(root)
. 그러나 사각형의 실제 움직임은 실제로는 느립니다 (초당 약 5 회의 움직임). self.window.master.after()
시간은 효과가없는 것처럼 보입니다.
이전에 다른 tkinter 프로젝트에서 저는 정말 반응이 빠른 tkinter 움직임을 얻을 수있었습니다. 그래서이 경우에는 시간 이동 업데이트를 최소화 할 수있는 방법이 있는지 궁금 해서요. 다른 스타일의 OOP를 사용하거나, 또는 완전히 다른 코드 일 수 있습니다.
업데이트 : .after()
메서드의 시간은 중요하지만 실제로는 메서드의 실시간에 스택됩니다. 왜 .after()
방법이 너무 오래 걸리는 것을 :
>>> print(timeit.timeit("(self.window.master.after(2, self.mom_calc))", number=10000, globals={"self":self}))
0.5395521819053108
그래서 내가 진짜 문제는 추측 : 메서드를 호출 시간
timeit
을 사용한 후,이 출력있어?
업데이트 2 : 여러 대의 컴퓨터에서 테스트되었는데, 어떤 플랫폼에서도 동작이 느립니다.
이것은 probem을 설명하기 위해 필요한보다 훨씬 더 많은 코드처럼 보이는 : 여기
는 (약간) 수정 된 코드입니다. 아래의 조언을 읽고 따르십시오 : [최소한의 완전하고 검증 가능한 예제를 만드는 방법] (http://stackoverflow.com/help/mcve), 또는 질문을 http://codereview.stackexchange.com/으로 이동하십시오. –지금 최소화했습니다. – Dova
"최소, 완료 및 확인 가능"의 "완료"부분을 잊어 버렸습니다. –