Python Tkinter coords函数不会在循环内移动画布对象 [英] Python Tkinter coords function not moving canvas objects inside loop

查看:48
本文介绍了Python Tkinter coords函数不会在循环内移动画布对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,可以从文本文件中读取位置,进行解析,然后使用coords函数将各个对象移动到tkinter画布上列出的位置.正在从文件中读取数据并正确地对其进行了解析,但是出于某种原因,coords函数仅在循环的最后一次迭代中将对象移动到文件中列出的最后位置.

I have a function that reads locations from a text file, parses them, then moves the respective objects to the locations listed on a tkinter canvas using the coords function. The data is being read from the file and parsed correctly but for some reason the coords function is only moving the objects to the last location listed in the file on the last iteration of the loop.

每次循环后是否都需要以某种方式更新画布?谢谢!

Do I need to update the canvas somehow after each iteration of my loop? Thanks!

这是我的代码:

def playback():
    fptr = tkFileDialog.askopenfilename()
    filename = open(fptr,"rU")
    if not filename:
        return

    stat.set('REPLAY IN PROGRESS')
    gamestatus[0] = 2
    for line in filename:
        line = line.strip()

        #Example line from input file: 'B:#,#,#,#|L:#,#,#,#|R:#,#,#,#'
        line = line.split('|')
        B_loc = line[0].split(':')[1].split(',')
        L_loc = line[1].split(':')[1].split(',')
        R_loc = line[2].split(':')[1].split(',')

        #Converting strings to ints and lists to tuples to simplify code below      
        B_tup=(int(B_loc[0]),int(B_loc[1]),int(B_loc[2]),int(B_loc[3]))
        L_tup=(int(L_loc[0]),int(L_loc[1]),int(L_loc[2]),int(L_loc[3]))
        R_tup=(int(R_loc[0]),int(R_loc[1]),int(R_loc[2]),int(R_loc[3]))

        #Moving objects to locations from input file        
        playingField.coords(pongball.ball,B_tup)
        playingField.coords(leftpaddle.paddle,L_tup)
        playingField.coords(rightpaddle.paddle,R_tup)
        time.sleep(.02)

    filename.close()
    gamestatus[0] = 0
    stat.set('-------Pong-------')

推荐答案

GUI开发中的一个很好的经验法则是永远不要调用 sleep .这冻结了GUI,即使只是几毫秒,仍然是一个不好的做法.

A very good rule of thumb in GUI development is to never call sleep. This freezes the GUI, and even if it's just for a few milliseconds it's still a bad practice.

在Tkinter中制作动画的正确方法是编写一个显示单个帧的函数,然后使用 after 重新安排自身的时间.这样一来,事件循环就可以在制作动画时不断为事件提供服务.

The proper way to do animation in Tkinter is to write a function that displays a single frame, then reschedules itself using after. This allows the event loop to constantly service events while doing the animation.

例如,将for语句的整个内容-减去sleep-并将其放入方法中.我们称其为刷新".让此函数使用after来重新安排自身的时间,例如:

For example, take the entire body of the for statement -- minus the sleep -- and put it into a method. Let's call this "refresh". Have this function re-schedule itself using after, like this:

def refresh():
    line = get_next_line()
    line = line.split('|')
    B_loc = line[0].split(':')[1].split(',')
    ...

    # call this function again in 20ms
    root.after(20, refresh)

现在您所要做的就是将 get_next_line 实现为一个函数,您已设置好.每当您更新坐标时,这将自动允许GUI重绘自身.

Now all you need to do is implement get_next_line as a function and you're set. This will automatically allow the GUI to redraw itself each time yo update the coordinates.

当然,您需要检查输入何时用尽,并且您可能希望拥有一个用户可以通过请求动画停止的按钮来设置的标志,等等.

Of course, you'll need to put in checks for when input is exhausted, and you might want to have a flag the user can set via a button that requests that the animation stops, etc.

这篇关于Python Tkinter coords函数不会在循环内移动画布对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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