【问题标题】:moving circle using tkinter使用 tkinter 移动圆圈
【发布时间】:2018-05-30 16:22:07
【问题描述】:

我是编程新手,目前正在尝试用产生气泡的烧杯进行化学刺激。经过大量研究,我设法让一个圆圈移动,并创建了一个代表烧杯的矩形。但是我希望在烧杯顶部形成圆圈并随机向上移动并在遇到我无法弄清楚的顶部时自行摧毁。如果有人可以帮助我,我将不胜感激。先感谢您。

我的代码是:

from tkinter import *

x = 10
y = 10
a = 50
b = 50

x_vel = 5
y_vel = 5

def move():

    global x
    global y
    global x_vel
    global y_vel
    if x < 0:
        x_vel = 5
    if x > 350:
        x_vel = -5
    if y < 0:
        y_vel = 5
    if y > 150:
        y_vel = -5
    canvas1.move(circle, x_vel, y_vel)
    coordinates = canvas1.coords(circle)
    x = coordinates[0]
    y = coordinates[1]
    window.after(33, move)

window   = Tk()
window.geometry("1000x1000")

canvas1=Canvas(window, height = 1000, width= 1000)
canvas1.grid (row=0, column=0, sticky=W)
coord = [x, y, a, b ]
circle = canvas1.create_oval(coord, outline="red", fill="red")


coord = [230, 270, 270, 310]
rect2 = canvas1.create_rectangle(coord, outline="Blue", fill="Blue")

move()

window.mainloop ()

【问题讨论】:

  • 不是 tkinter,而是类似于灵感的东西...mpl rain animation.
  • 使用按钮{} 来正确格式化代码。现在你有错误的缩进,我们不能运行它。
  • 我更正了我的缩进

标签: python tkinter


【解决方案1】:

使用random可以随意移动它。

如果y &lt; -height 则对象左侧屏幕,您可以将其移动到起始位置。

import tkinter as tk
import random

def move():
    global x
    global y
    global x_vel
    global y_vel

    # get random move
    x_vel = random.randint(-5, 5)
    y_vel = -5

    canvas1.move(circle, x_vel, y_vel)
    coordinates = canvas1.coords(circle)

    x = coordinates[0]
    y = coordinates[1]

    # if outside screen move to start position
    if y < -height:
        x = start_x
        y = start_y
        canvas1.coords(circle, x, y, x+width, y+height)

    window.after(33, move)

# --- main ---

start_x = 230
start_y = 270

x = start_x
y = start_y

width  = 50
height = 50

x_vel = 5
y_vel = 5

window = tk.Tk()
window.geometry("1000x1000")

canvas1 = tk.Canvas(window, height=1000, width=1000)
canvas1.grid(row=0, column=0, sticky='w')

coord = [x, y, x+width, y+height]
circle = canvas1.create_oval(coord, outline="red", fill="red")

coord = [x, y, x+40, y+40]
rect2 = canvas1.create_rectangle(coord, outline="Blue", fill="Blue")

move()

window.mainloop ()

编辑:使用类您可以轻松拥有许多项目

import tkinter as tk
import random

class Bubble():

    def __init__(self, canvas, x, y, size, color='red'):
        self.canvas = canvas

        self.x = x
        self.y = y

        self.start_x = x
        self.start_y = y

        self.size = size
        self.color = color

        self.circle = canvas.create_oval([x, y, x+size, y+size], outline=color, fill=color)

    def move(self):
        x_vel = random.randint(-5, 5)
        y_vel = -5

        self.canvas.move(self.circle, x_vel, y_vel)
        coordinates = self.canvas.coords(self.circle)

        self.x = coordinates[0]
        self.y = coordinates[1]

        # if outside screen move to start position
        if self.y < -self.size:
            self.x = self.start_x
            self.y = self.start_y
            self.canvas.coords(self.circle, self.x, self.y, self.x + self.size, self.y + self.size)

def move():
    for item in bubbles:
        item.move()

    window.after(33, move)

# --- main ---

start_x = 230
start_y = 270

window = tk.Tk()
window.geometry("1000x1000")

canvas = tk.Canvas(window, height=1000, width=1000)
canvas.grid(row=0, column=0, sticky='w')

bubbles = []
for i in range(5):
    offset = random.randint(10, 20)
    b = Bubble(canvas, start_x+10, start_y-offset, 20, 'red')
    bubbles.append(b)
for i in range(5):
    offset = random.randint(0, 10)
    b = Bubble(canvas, start_x+10, start_y-offset, 20, 'green')
    bubbles.append(b)

coord = [start_x, start_y, start_x+40, start_y+40]
rect = canvas.create_rectangle(coord, outline="Blue", fill="Blue")

move()

window.mainloop ()

【讨论】:

猜你喜欢
  • 2018-09-04
  • 1970-01-01
  • 2011-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-27
相关资源
最近更新 更多