赞
踩
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()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。