我如何使用“睡眠"?适当地? [英] How can I use "sleep" properly?

查看:40
本文介绍了我如何使用“睡眠"?适当地?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我知道睡眠功能,但它并没有像我预期的那样工作.如果我这样做:

So, I know about the sleep function, but it doesn't work how I expected it to work. If I do something like this:

from time import sleep
print('Something')
sleep (10)
print('Something')

它以(我认为)应该的方式工作(它打印一件事,等待然后打印另一件事).

It works how (I think) it should (it prints one thing, waits and then prints the other one).

但在我的代码中它不是那样工作的.这是整个代码:

But in my code it doesn't work like that. This is the whole code:

from tkinter import *
from time import sleep
# Fenster
window = Tk()
window.title('Test')
c = Canvas(window, height=450, width=800)
c.pack()

# Bildelemente
Hintergrund = PhotoImage(file='Hintergrund.gif')
Background = Label(image=Hintergrund)
Background.image = Hintergrund
Background.place(x=0, y=0)

Boden = PhotoImage(file='Boden.gif')
Ground = Label(image=Boden)
Ground.image = Boden
Ground.place(x=0, y=300)

Char = PhotoImage(file='Char.gif')
Character = Label(image=Char)
Character.image = Char
Character.pack()

# Koordinaten ermitteln
def coordinatesX(id_num):
    pos = c.coords(id_num)
    x = (pos[0] + pos[2])/2
    return x

def coordinatesY(id_num):
    pos = c.coords(id_num)
    y = (pos[1] + pos[3])/2
    return y

# Charakter bewegen
def Char_move(event):
    if event.keysym == 'Right' and coordinatesX(Char_) < 800 - 50:
        c.move(Char_, 10, 0)
        Character.place(x=(coordinatesX(Char_)), y=(coordinatesY(Char_)))
    if event.keysym == 'Left' and coordinatesX(Char_) > 0:
        c.move(Char_, -10, 0)
        Character.place(x=(coordinatesX(Char_)), y=(coordinatesY(Char_)))
    if event.keysym == 'Up' and coordinatesY(Char_) < 300 and coordinatesY(Char_) > 0:
        jump()
        sleep(5)
        print('NA')

c.bind_all('<Key>', Char_move)

def jump():
    c.move(Char_, 0, -50)
    Character.place(x=(coordinatesX(Char_)), y=(coordinatesY(Char_)))

# Objekt zur Postitionsbestimmung vom Charakter
Char_ = c.create_oval(0, 0, 50, 50, fill='red')
c.move(Char_, 100, 223)
Character.place(x=(coordinatesX(Char_)), y=(coordinatesY(Char_)))

在这部分我希望它等待然后做一些事情(在这个示例"打印中):

In this part I want it to wait and then do something (in this "example" print):

jump()
sleep(5)
print('NA')

但是当我运行程序并点击向上"时,它会等待,然后对象向上移动,同时程序会打印NA".我该怎么做才能让对象上升,等待然后打印一些东西?

But when I run the program and hit 'Up', it waits and then the object goes up and the program prints "NA" at the same time. How do I have to do this so that the object goes up, it waits and then prints something?

推荐答案

不要使用睡眠.改用 tkinter after 方法.

Do not use sleep. Use the tkinter after method instead.

jump()
window.after(5000, lambda x: print('NA'))

睡眠会冻结您的 gui.Jump 被执行,但 sleep 会阻止 gui 被重绘,因此你看不到任何变化.After 是来自 Tk 的一个方法,允许您安排操作.

Sleep freezes your gui. Jump is executed but sleep prevents the gui from being redrawn, therefore you do not see any change. After is a method from Tk and allows you to schedule operations.

这篇关于我如何使用“睡眠"?适当地?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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