画布中的python时间睡眠方法

编程入门 行业动态 更新时间:2024-10-18 08:26:46
本文介绍了画布中的python时间睡眠方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 06:40:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390657.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:画布   睡眠   时间   方法   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!