当前位置:   article > 正文

利用Python制作跨年滚条

利用Python制作跨年滚条

import tkinter as tk
import random


def shift():
    global change_color
    x1, y1, x2, y2 = canvas.bbox("marquee")
    if(x2 < 0):
        x1 = canvas.winfo_width()
        y1 = canvas.winfo_height()//2
        canvas.coords(text, x1, 2)
    else:
        canvas.move(text, -2, 0)
        change_color = (change_color+1) % 25
        if change_color == 0:
            canvas.itemconfig(text, fill=random.choice(colors))
    canvas.after(1000//fps, shift)


app = tk.Tk()
app.title('恭贺新年')
canvas = tk.Canvas(app, bg='black')
canvas.pack(fill=tk.BOTH, expand=1)
colors = ('red', 'magenta', 'blue', 'cyan',
          'green', 'white', 'yellow', 'white')
text_var = "Happy New Year. 2024新年快乐,元旦快乐!"
text = canvas.create_text(0, -2000, text=text_var, font=('Times New Roman',
                          20, 'bold'), fill='white', tags="marquee", anchor='nw')
x1, y1, x2, y2 = canvas.bbox("marquee")
width = x2-x1
height = y2-y1
canvas['width'] = width-300
canvas['height'] = height+5
canvas.moveto(text, width-300, 2)
fps = 40  # 控制移动的速度
change_color = 0  # 控制颜色变化
shift()
app.mainloop()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/93140
推荐阅读
相关标签
  

闽ICP备14008679号