python+tkinter+canvas实现天降棒棒糖,生活甜甜蜜蜜

python+tkinter+canvas实现天降棒棒糖,生活甜甜蜜蜜。

一、先看看效果吧。

在这里插入图片描述
直接开始介绍了吧。

二、准备资源图片

在这里插入图片描述
通过ps或者ppt软件把这个图片抠图干净,就会出来的效果好看些。

三、实现逻辑

(一)自定义棒棒糖类

class Ball:

    def __init__(self, canvas,root):
        self.canvas = canvas
        self.root = root
        self.image = Image.open('bbt.png').resize((30, 30))

    def create_ball(self):

        self.xpos = randint(50,self.root.winfo_width()-50)
        self.ypos = randint(-self.root.winfo_height(),0)
        self.vect = randint(1,3)

        self.tkimage = ImageTk.PhotoImage(self.image)
        self.canvas_obj = canvas.create_image(self.xpos, self.ypos, image=self.tkimage)


    def move_ball(self):
        # self.xpos += 1
        self.ypos += self.vect
        if self.ypos > self.root.winfo_height():
            self.xpos = randint(50, self.root.winfo_width() - 50)
            self.ypos = randint(-50, 0)
            self.vect = randint(1,3)
        self.tkimage = ImageTk.PhotoImage(self.image)
        self.canvas_obj = canvas.create_image(self.xpos, self.ypos, image=self.tkimage)

(二)随机生成棒棒糖实体

    balls = []
    num = 50
    for i in range(num):
        ball = Ball(canvas, tk)
        ball.create_ball()
        balls.append(ball)

(三)不断update位置信息

    move_balls()
    def move_balls():
    for ball in balls:
        ball.move_ball()
    canvas.after(20, move_balls)

(四)让窗体透明

def on_resize(evt):
    tk.configure(width=evt.width,height=evt.height)
    canvas.create_rectangle(0, 0, canvas.winfo_width(), canvas.winfo_height(), fill=TRANSCOLOUR, outline=TRANSCOLOUR)

四、完整代码

from tkinter import *
from random import *
import math
from PIL import Image,ImageTk

class Ball:

    def __init__(self, canvas,root):
        self.canvas = canvas
        self.root = root
        self.image = Image.open('bbt.png').resize((30, 30))

    def create_ball(self):

        self.xpos = randint(50,self.root.winfo_width()-50)
        self.ypos = randint(-self.root.winfo_height(),0)
        self.vect = randint(1,3)

        self.tkimage = ImageTk.PhotoImage(self.image)
        self.canvas_obj = canvas.create_image(self.xpos, self.ypos, image=self.tkimage)


    def move_ball(self):
        # self.xpos += 1
        self.ypos += self.vect
        if self.ypos > self.root.winfo_height():
            self.xpos = randint(50, self.root.winfo_width() - 50)
            self.ypos = randint(-50, 0)
            self.vect = randint(1,3)
        self.tkimage = ImageTk.PhotoImage(self.image)
        self.canvas_obj = canvas.create_image(self.xpos, self.ypos, image=self.tkimage)

def move_balls():
    for ball in balls:
        ball.move_ball()
    canvas.after(20, move_balls)


def on_resize(evt):
    tk.configure(width=evt.width,height=evt.height)
    canvas.create_rectangle(0, 0, canvas.winfo_width(), canvas.winfo_height(), fill=TRANSCOLOUR, outline=TRANSCOLOUR)
    # canvas.create_oval(240, 30, 330, 200,
    #                    outline='yellow',  # 边框颜色
    #                    fill='pink',  # 填充颜色
    #                    width=4  # 边框宽度
    #                    )
    print(canvas.winfo_width())


if __name__ == '__main__':

    TRANSCOLOUR = 'gray'

    tk = Tk()
    tk.geometry('800x600+300+100')
    tk.title('天降棒棒糖咯,生活甜甜蜜蜜咯!!!')
    tk.wm_attributes('-transparentcolor', TRANSCOLOUR)
    tk.wm_attributes("-topmost", 1)
    tk.bind('<Configure>', on_resize)
    # tk.overrideredirect(1)

    canvas = Canvas(tk)
    tk.winfo_width()
    canvas.pack(fill=BOTH, expand=Y)
    # canvas.config(highlightthickness=0)
    tk.update()

    balls = []
    num = 50
    for i in range(num):
        ball = Ball(canvas, tk)
        ball.create_ball()
        balls.append(ball)

    move_balls()

    tk.mainloop()

五、运行效果

在这里插入图片描述
好吧,写完,希望对你有帮忙。

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,需要导入相关的库文件: ```python import tkinter as tk from tkinter import filedialog from PIL import ImageTk, Image import numpy as np import cv2 ``` 然后,创建一个窗口,并且定义一个函数用于选择图片: ```python root = tk.Tk() root.title("Lane Detection") def select_image(): global img_pil, img_tk, img_gray path = filedialog.askopenfilename() img_pil = Image.open(path) img_tk = ImageTk.PhotoImage(img_pil) img_gray = np.asarray(img_pil.convert('L')) canvas.itemconfig(image_on_canvas, image=img_tk) ``` 接下来,创建一个画布和一个按钮,用于选择图片: ```python canvas = tk.Canvas(root, width=500, height=500) canvas.pack(side=tk.LEFT) button = tk.Button(root, text="Select Image", command=select_image) button.pack(side=tk.RIGHT) ``` 然后,定义一个函数用于实现车道线边缘检测: ```python def edge_detection(): global img_pil, img_tk, img_gray kernel = np.array([[-1,0,1],[-1,0,1],[-1,0,1]]) img_edges = cv2.filter2D(img_gray, -1, kernel) img_edges = cv2.convertScaleAbs(img_edges) img_pil = Image.fromarray(img_edges) img_tk = ImageTk.PhotoImage(img_pil) canvas.itemconfig(image_on_canvas, image=img_tk) ``` 最后,在画布上显示图片: ```python img_tk = ImageTk.PhotoImage(Image.new('RGB', (500, 500), (255, 255, 255))) image_on_canvas = canvas.create_image(0, 0, image=img_tk, anchor=tk.NW) ``` 完整代码如下: ```python import tkinter as tk from tkinter import filedialog from PIL import ImageTk, Image import numpy as np import cv2 root = tk.Tk() root.title("Lane Detection") def select_image(): global img_pil, img_tk, img_gray path = filedialog.askopenfilename() img_pil = Image.open(path) img_tk = ImageTk.PhotoImage(img_pil) img_gray = np.asarray(img_pil.convert('L')) canvas.itemconfig(image_on_canvas, image=img_tk) def edge_detection(): global img_pil, img_tk, img_gray kernel = np.array([[-1,0,1],[-1,0,1],[-1,0,1]]) img_edges = cv2.filter2D(img_gray, -1, kernel) img_edges = cv2.convertScaleAbs(img_edges) img_pil = Image.fromarray(img_edges) img_tk = ImageTk.PhotoImage(img_pil) canvas.itemconfig(image_on_canvas, image=img_tk) canvas = tk.Canvas(root, width=500, height=500) canvas.pack(side=tk.LEFT) button = tk.Button(root, text="Select Image", command=select_image) button.pack(side=tk.RIGHT) img_tk = ImageTk.PhotoImage(Image.new('RGB', (500, 500), (255, 255, 255))) image_on_canvas = canvas.create_image(0, 0, image=img_tk, anchor=tk.NW) button_edge_detection = tk.Button(root, text="Edge Detection", command=edge_detection) button_edge_detection.pack(side=tk.BOTTOM) root.mainloop() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值