Python 3.x 如何使多个椭圆在画布上移动?

Python 3.x 如何使多个椭圆在画布上移动?,python-3.x,tkinter,Python 3.x,Tkinter,我的问题是如何让多个“泡泡”在屏幕上随机移动 我需要做大量的def bubble():还是有更简单的方法?在画布上随意移动“泡泡”也让我感到困惑 迄今为止的编码: from tkinter import * import random def quit(): root.destroy() def bubble(): xval = random.randint(5,765) yval = random.randint(5,615) canvas.create_

我的问题是如何让多个“泡泡”在屏幕上随机移动

我需要做大量的def bubble():还是有更简单的方法?在画布上随意移动“泡泡”也让我感到困惑

迄今为止的编码:

from tkinter import *
import random

def quit():
    root.destroy()


def bubble():
    xval = random.randint(5,765)
    yval = random.randint(5,615)
    canvas.create_oval(xval,yval,xval+30,yval+30, fill="#00ffff",outline="#00bfff",width=5)
    canvas.create_text(xval+15,yval+15,text=number)
    canvas.update()

def main():
    global root
    global tkinter
    global canvas
    root = Tk()
    root.title("Math Bubbles")
    Button(root, text="Quit", width=8, command=quit).pack()
    Button(root, text="Start", width=8, command=bubble).pack()
    canvas = Canvas(root, width=800, height=650, bg = '#afeeee')
    canvas.pack()
    root.mainloop()

# Create a sequence of numbers to choose from. CAPS denotes a constant variable
NUMBERS = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)

# Pick numbers randomly from the sequence with the help of a random.choice function
number = random.choice(NUMBERS)

# Create a variable to use later to see if the guess is correct
correct = number

main()

这应该会让你开始:

import Tkinter, random

class BubbleFrame:

    def __init__(self, root):
        root.title("Math Bubbles")
        Tkinter.Button(root, text="Add Bubbles", width=8, command=self.bubble).pack()
        Tkinter.Button(root, text="Quit", width=8, command=quit).pack()
        self.canvas = Tkinter.Canvas(root, width=800, height=650, bg = '#afeeee')
        self.canvas.pack()
        self.bubbles = {} # this will hold bubbles ids, positions and velocities

    def bubble(self):
        # add bubbles for numbers from 1 to 20
        for number in range(1, 20+1):
            xval = random.randint(5,765)
            yval = random.randint(5,615)
            s1 = self.canvas.create_oval(xval,yval,xval+30,yval+30, fill="#00ffff",outline="#00bfff",width=5)
            s2 = self.canvas.create_text(xval+15,yval+15, text=number)
            self.bubbles[(s1, s2)] = (xval, yval, 0, 0) # add bubbles to dictionary

    def loop(self, root):
        for (s1, s2), (x, y, dx, dy) in self.bubbles.items():
            # update velocities and positions
            dx += random.randint(-1, 1)
            dy += random.randint(-1, 1)
            # dx and dy should not be too large
            dx, dy = max(-5, min(dx, 5)), max(-5, min(dy, 5))
            # bounce off walls
            if not 0 < x < 770: dx = -dx
            if not 0 < y < 620: dy = -dy
            # apply new velocities
            self.canvas.move(s1, dx, dy)
            self.canvas.move(s2, dx, dy)
            self.bubbles[(s1, s2)] = (x + dx, y + dy, dx, dy)
        # have mainloop repeat this after 100 ms
        root.after(100, self.loop, root)

if __name__ == "__main__":
    root = Tkinter.Tk()
    frame = BubbleFrame(root)
    frame.loop(root)
    root.mainloop()
导入Tkinter,随机
类BubbleFrame:
定义初始化(自,根):
root.title(“数学泡泡”)
按钮(root,text=“添加气泡”,宽度=8,命令=self.bubble).pack()
按钮(root,text=“Quit”,width=8,command=Quit).pack()
self.canvas=Tkinter.canvas(根,宽度=800,高度=650,bg='#afeee')
self.canvas.pack()
self.bubbles={}#这将保存气泡ID、位置和速度
def气泡(自):
#为1到20之间的数字添加气泡
对于范围(1、20+1)内的数字:
xval=random.randint(5765)
yval=random.randint(5615)
s1=self.canvas.create_oval(xval,yval,xval+30,yval+30,fill=“#00ffff”,outline=“#00bfff”,width=5)
s2=self.canvas.create_text(xval+15,yval+15,text=number)
self.bubbles[(s1,s2)]=(xval,yval,0,0)#将bubbles添加到字典中
def循环(自我,根):
对于self.bubbles.items()中的(s1,s2),(x,y,dx,dy):
#更新速度和位置
dx+=random.randint(-1,1)
dy+=random.randint(-1,1)
#dx和dy不应太大
dx,dy=max(-5,min(dx,5)),max(-5,min(dy,5))
#从墙上弹下来
如果不是0
请注意,我对代码进行了一些重构,使用了一个类来包装这些方法和属性,但这完全取决于您。还要注意,我已经用Python2.7实现了这一点,因此可能需要进行一些小的修改,例如Tkinter包的名称

您不需要为每个要添加的气泡定义另一个
bubble
函数——事实上,您的代码已经向画布添加了许多气泡,所以这一部分很好。棘手的一点是移动气泡


为此,我首先添加了
self.bubbles
字典,将气泡的ID及其标签(由
canvas.create…
方法返回)映射到它们的当前位置和速度。最后,循环方法更新气泡的位置和速度,并重新绘制画布。最后,此方法将使用
after
方法安排下一次执行。

这将使您开始:

import Tkinter, random

class BubbleFrame:

    def __init__(self, root):
        root.title("Math Bubbles")
        Tkinter.Button(root, text="Add Bubbles", width=8, command=self.bubble).pack()
        Tkinter.Button(root, text="Quit", width=8, command=quit).pack()
        self.canvas = Tkinter.Canvas(root, width=800, height=650, bg = '#afeeee')
        self.canvas.pack()
        self.bubbles = {} # this will hold bubbles ids, positions and velocities

    def bubble(self):
        # add bubbles for numbers from 1 to 20
        for number in range(1, 20+1):
            xval = random.randint(5,765)
            yval = random.randint(5,615)
            s1 = self.canvas.create_oval(xval,yval,xval+30,yval+30, fill="#00ffff",outline="#00bfff",width=5)
            s2 = self.canvas.create_text(xval+15,yval+15, text=number)
            self.bubbles[(s1, s2)] = (xval, yval, 0, 0) # add bubbles to dictionary

    def loop(self, root):
        for (s1, s2), (x, y, dx, dy) in self.bubbles.items():
            # update velocities and positions
            dx += random.randint(-1, 1)
            dy += random.randint(-1, 1)
            # dx and dy should not be too large
            dx, dy = max(-5, min(dx, 5)), max(-5, min(dy, 5))
            # bounce off walls
            if not 0 < x < 770: dx = -dx
            if not 0 < y < 620: dy = -dy
            # apply new velocities
            self.canvas.move(s1, dx, dy)
            self.canvas.move(s2, dx, dy)
            self.bubbles[(s1, s2)] = (x + dx, y + dy, dx, dy)
        # have mainloop repeat this after 100 ms
        root.after(100, self.loop, root)

if __name__ == "__main__":
    root = Tkinter.Tk()
    frame = BubbleFrame(root)
    frame.loop(root)
    root.mainloop()
导入Tkinter,随机
类BubbleFrame:
定义初始化(自,根):
root.title(“数学泡泡”)
按钮(root,text=“添加气泡”,宽度=8,命令=self.bubble).pack()
按钮(root,text=“Quit”,width=8,command=Quit).pack()
self.canvas=Tkinter.canvas(根,宽度=800,高度=650,bg='#afeee')
self.canvas.pack()
self.bubbles={}#这将保存气泡ID、位置和速度
def气泡(自):
#为1到20之间的数字添加气泡
对于范围(1、20+1)内的数字:
xval=random.randint(5765)
yval=random.randint(5615)
s1=self.canvas.create_oval(xval,yval,xval+30,yval+30,fill=“#00ffff”,outline=“#00bfff”,width=5)
s2=self.canvas.create_text(xval+15,yval+15,text=number)
self.bubbles[(s1,s2)]=(xval,yval,0,0)#将bubbles添加到字典中
def循环(自我,根):
对于self.bubbles.items()中的(s1,s2),(x,y,dx,dy):
#更新速度和位置
dx+=random.randint(-1,1)
dy+=random.randint(-1,1)
#dx和dy不应太大
dx,dy=max(-5,min(dx,5)),max(-5,min(dy,5))
#从墙上弹下来
如果不是0
请注意,我对代码进行了一些重构,使用了一个类来包装这些方法和属性,但这完全取决于您。还要注意,我已经用Python2.7实现了这一点,因此可能需要进行一些小的修改,例如Tkinter包的名称

您不需要为每个要添加的气泡定义另一个
bubble
函数——事实上,您的代码已经向画布添加了许多气泡,所以这一部分很好。棘手的一点是移动气泡


为此,我首先添加了
self.bubbles
字典,将气泡的ID及其标签(由
canvas.create…
方法返回)映射到它们的当前位置和速度。最后,循环方法更新气泡的位置和速度,并重新绘制画布。最后,此方法将使用
after
方法安排下一次执行。

线程为简单动画增加了太多的复杂性。另外,由于tkinter不是线程安全的,因此几乎肯定会意外崩溃。除了创建小部件的线程外,您不能从任何线程可靠地调用小部件方法