画布中的python时间睡眠方法 [英] python time sleep method in canvas

查看:34
本文介绍了画布中的python时间睡眠方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我想要做的.非常简单的代码.从一点到另一点逐渐画线.这是我的代码:

This is what I am trying to do. Very simple code. Draw line from one point to another gradually. Here's my code:

from Tkinter import *
import time

mGui = Tk()
mGui.title('GUI Example')
mGui.geometry('1024x576+10+30')

#Canvas
canvas_1 = Canvas(mGui,height=500,width=500,bg='grey')

for i in range(1, 500):
    time.sleep(0.5)
    canvas_1.create_line(0,0,i,i)
    canvas_1.update()

canvas_1.pack()

mGui.mainloop()

我的窗口只保持空白 0.5*500 秒(250 秒),然后画布显示绘制的线条.我究竟做错了什么?

My window just stays blank white 0.5*500 seconds (250 seconds) and then canvas shows up with the line drawn. What am I doing wrong?

推荐答案

您需要在开始循环之前调用画布上的 pack 方法:

You need to call the pack method on the canvas before you start the loop:

from Tkinter import *
import time

mGui = Tk()
mGui.title('GUI Example')
mGui.geometry('1024x576+10+30')

#Canvas
canvas_1 = Canvas(mGui,height=500,width=500,bg='grey')

#################
canvas_1.pack()
#################

for i in range(1, 500):
    time.sleep(0.5)
    canvas_1.create_line(0,0,i,i)
    canvas_1.update()

mGui.mainloop()

否则,直到循环退出后,画布才会放置在窗口上.

Otherwise, the canvas will not be placed on the window until after the loop exits.

这篇关于画布中的python时间睡眠方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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