Tkinter 理解 after() [英] Tkinter understanding after()

查看:66
本文介绍了Tkinter 理解 after()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,在这里看看我之前的帖子:Tkinter 理解 mainloop

First of all, take a look at my previous thread here: Tkinter understanding mainloop

遵循那里的建议后,在 GUI 编程中,必须不惜一切代价避免无限循环,以保持小部件对用户输入的响应.

After following the advice from there, in GUI programming, infinite loops have to be avoided at all costs, in order to keep the widgets responsive to user input.

而不是使用:

while 1:
    ball.draw()
    root.update()
    time.sleep(0.01)

我在我的 draw() 函数中使用了 self.canvas.after(1, self.draw).

I managed using self.canvas.after(1, self.draw) inside my draw() function.

所以我的代码现在看起来像这样:

So my code now looks like this:

# Testing skills in game programming

from Tkinter import *

root = Tk()
root.title("Python game testing")
root.resizable(0, 0)
root.wm_attributes("-topmost", 1)

canvas = Canvas(root, width=500, height=400, bd=0, highlightthickness=0)
canvas.pack()
root.update()

class Ball:
    def __init__(self, canvas, color):
        self.canvas = canvas
        self.id = canvas.create_oval(10, 10, 25, 25, fill=color)
        self.canvas.move(self.id, 245, 100)

        self.canvas_height = canvas.winfo_height()
        self.x = 0
        self.y = -1

    def draw(self):
        self.canvas.move(self.id, self.x, self.y)

        pos = self.canvas.coords(self.id)
        if pos[1] <= 0:
            self.y = 1
        if pos[3] >= self.canvas_height:
            self.y = -1

        self.canvas.after(2, self.draw)


ball = Ball(canvas, "red")
ball.draw()

root.mainloop()

然而,self.canvas.after() 中的时间无法正常工作......如果设置为 1,它非常快!如果设置为10、5甚至2,就太慢了!在我的代码中使用上述 while 循环时我没有遇到这个问题,因为 time.sleep() 可以正常工作!

However, time inside self.canvas.after() does not work properly... If set to 1 it's extremely fast! If it's set to 10, 5 or even 2, it's too slow! I didn't have that problem while using the above while loop in my code since the time.sleep() worked as it should!

我现在可以报告 Tkinter 的 after 函数中的时间在我的 Windows 8.1 平板电脑和我的 Windows 8.1 笔记本电脑中无法正常工作,而在同一台笔记本电脑上通过虚拟机运行 Ubuntu 它确实可以正常工作.

I can now report that the time inside the after function of Tkinter does not work properly in my Windows 8.1 tablet, and in my Windows 8.1 laptop, while in the same laptop when running Ubuntu through virtual machine it does work as it should.

推荐答案

对象(画布)的速度和时钟/循环速度应该被视为两个不同的东西,恕我直言.因此,您可以在 2ms 甚至更长时间后离开循环,例如 10...25...

Speed of the object (canvas) and clock/loop speed should be considered as two different things, IMHO. Hence, you can leave loop after 2ms or even larger like 10...25...

...
self.canvas.after(2, self.draw)
... # loop this after 2 ms.

同时,根据多少像素"改变速度:

while, changing speed in terms of 'how many pixels' :

pos = self.canvas.coords(self.id)
    if pos[1] <= 0:
        self.y = 20
    if pos[3] >= self.canvas_height:
        self.y = -20

因此调整这些值,并且:

so adjusting these values, and :

self.canvas.move(self.id, 245, 100)

可以让您微调红点的位置和速度.希望有帮助

can let you fine-tune position and speed of your red dot. Hope it helps

这篇关于Tkinter 理解 after()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆