移动球,Tkinter的画布 [英] Moving balls in Tkinter Canvas

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

问题描述

这是我将要进行两个移动球,一个非常基本的程序,但其中只有一个真正移动。

我已经尝试了一些变化,以及,但无法获得第二球移动;另一个相关的问题 - 有些人使用移动(对象)的方法来实现这一目标,而其他人做了删除(对象)然后再重新绘制。哪一个我应该使用,为什么?

这是我的code,它只是动画/移动一个球:

 从Tkinter的进口*类球:
    高清__init __(自我,帆布,X1,Y1,X2,Y2):
    self.x1 = X1
    self.y1 = Y1
    self.x2 = X2
    self.y2 = Y2
    self.canvas =帆布
    self.ball = canvas.create_oval(self.x1,self.y1,self.x2,self.y2,补=红)    高清move_ball(个体经营):
        而真正的:
            self.canvas.move(self.ball,2,1)
            self.canvas.after(20)
            self.canvas.update()#初始化根窗口和画布
根= TK()
root.title(球)
root.resizable(假,假)
帆布=帆布(根,宽度= 300,高度= 300)
canvas.pack()#创建两个球对象,并为它们制作动画
球1 =球(帆布,10,10,30,30)
ball2 =球(帆布,60,60,80,80)ball1.move_ball()
ball2.move_ball()root.mainloop()


解决方案

您永远不应该把一个无限循环的GUI程序中 - 已经有一个无限循环运行。如果你希望你的球独立运动,简单地采取退出循环,并有 move_ball 方法把一个新的呼叫本身的事件循环。就这样,你的球会永远继续移动(这意味着你应该把一些检查在那里prevent这种情况发生)

我已经删除了无限循环,减缓动画了一下,还采用随机值,它们移动的方向稍加修改你的程序。所有这些变化是 move_ball 方法内。

 从Tkinter的进口*
从随机进口randint类球:
    高清__init __(自我,帆布,X1,Y1,X2,Y2):
        self.x1 = X1
        self.y1 = Y1
        self.x2 = X2
        self.y2 = Y2
        self.canvas =帆布
        self.ball = canvas.create_oval(self.x1,self.y1,self.x2,self.y2,补=红)    高清move_ball(个体经营):
        DELTAX = randint(0,5)
        DELTAY = randint(0,5)
        self.canvas.move(self.ball,DELTAX,DELTAY)
        self.canvas.after(50,self.move_ball)#初始化根窗口和画布
根= TK()
root.title(球)
root.resizable(假,假)
帆布=帆布(根,宽度= 300,高度= 300)
canvas.pack()#创建两个球对象,并为它们制作动画
球1 =球(帆布,10,10,30,30)
ball2 =球(帆布,60,60,80,80)ball1.move_ball()
ball2.move_ball()root.mainloop()

This is a very basic program with which I want to make two moving balls, but only one of them actually moves.

I have tried some variations as well but can't get the second ball moving; another related question - some people use the move(object) method to achieve this, while others do a delete(object) and then redraw it. Which one should I use and why?

This is my code that is only animating/moving one ball:

from Tkinter import *

class Ball:
    def __init__(self, canvas, x1, y1, x2, y2):
    self.x1 = x1
    self.y1 = y1
    self.x2 = x2
    self.y2 = y2
    self.canvas = canvas
    self.ball = canvas.create_oval(self.x1, self.y1, self.x2, self.y2, fill="red")

    def move_ball(self):
        while True:
            self.canvas.move(self.ball, 2, 1)
            self.canvas.after(20)
            self.canvas.update()

# initialize root Window and canvas
root = Tk()
root.title("Balls")
root.resizable(False,False)
canvas = Canvas(root, width = 300, height = 300)
canvas.pack()

# create two ball objects and animate them
ball1 = Ball(canvas, 10, 10, 30, 30)
ball2 = Ball(canvas, 60, 60, 80, 80)

ball1.move_ball()
ball2.move_ball()

root.mainloop()

解决方案

You should never put an infinite loop inside a GUI program -- there's already an infinite loop running. If you want your balls to move independently, simply take out the loop and have the move_ball method put a new call to itself on the event loop. With that, your balls will continue to move forever (which means you should put some sort of check in there to prevent that from happening)

I've modified your program slightly by removing the infinite loop, slowing down the animation a bit, and also using random values for the direction they move. All of that changes are inside the move_ball method.

from Tkinter import *
from random import randint

class Ball:
    def __init__(self, canvas, x1, y1, x2, y2):
        self.x1 = x1
        self.y1 = y1
        self.x2 = x2
        self.y2 = y2
        self.canvas = canvas
        self.ball = canvas.create_oval(self.x1, self.y1, self.x2, self.y2, fill="red")

    def move_ball(self):
        deltax = randint(0,5)
        deltay = randint(0,5)
        self.canvas.move(self.ball, deltax, deltay)
        self.canvas.after(50, self.move_ball)

# initialize root Window and canvas
root = Tk()
root.title("Balls")
root.resizable(False,False)
canvas = Canvas(root, width = 300, height = 300)
canvas.pack()

# create two ball objects and animate them
ball1 = Ball(canvas, 10, 10, 30, 30)
ball2 = Ball(canvas, 60, 60, 80, 80)

ball1.move_ball()
ball2.move_ball()

root.mainloop()

这篇关于移动球,Tkinter的画布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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