Python Tkinter动态爱心动画:用代码说谢谢大家
{"title":"Python Tkinter动态爱心动画:用代码说谢谢大家","description":"使用Python和Tkinter库,创建了一个在新窗口中显示动态“谢谢大家”文本并带有闪动爱心的代码示例。点击按钮即可触发动画,表达感谢之情。","keywords":"Python, Tkinter, 动画, 爱心, 谢谢大家, 代码示例, 动态文本, 新窗口, GUI, 用户界面","content":"import tkinter as tk\nfrom tkinter import ttk\n\ndef create_heart_animation(canvas, x, y, size, color):\n canvas.create_arc(x, y, x+size, y+size, start=150, extent=60, fill=color, outline='')\n canvas.create_arc(x+size, y, x+size2, y+size, start=210, extent=60, fill=color, outline='')\n canvas.create_polygon(x+size1.5, y+size0.2, x+size0.2, y+size1.5, x+size2.8, y+size*1.5, fill=color, outline='')\n\ndef show_message():\n new_window = tk.Toplevel(root)\n new_window.geometry('300x200')\n new_window.title('谢谢大家')\n\n canvas = tk.Canvas(new_window, width=300, height=200)\n canvas.pack()\n\n for i in range(100):\n canvas.delete('all') # 清空画布\n create_heart_animation(canvas, 100, 50, 100, 'red') # 绘制爱心\n canvas.create_text(150, 150, text='谢谢大家', font=('Helvetica', 20), fill='black') # 显示文本\n canvas.update()\n canvas.after(100) # 暂停100毫秒\n\nroot = tk.Tk()\nroot.geometry('200x100')\nroot.title('点击按钮')\n\nbutton = ttk.Button(root, text='点击', command=show_message)\nbutton.place(relx=0.5, rely=0.5, anchor='center')\n\nroot.mainloop()\n"}
原文地址: https://www.cveoy.top/t/topic/pJGr 著作权归作者所有。请勿转载和采集!