李峋同款python爱心代码
🤵♂️ 个人主页@阿飞个人主页
如果文章对你有帮助的话,欢迎评论 💬点赞👍🏻 收藏 📂加关注
1 前言
最近比较火的爱心代码,有很多版本,看到许多人好像有python环境问题的困扰,这里就改造一个小白也能玩懒人加强UI交互版
① UI界面交互,可更换爱心颜色,可加/更换表白对象标签
② 打包成exe执行文件,点击软件就能运行,不用安装python环境!
③名字标签随着心跳律动!
2 效果预览
视频效果 李峋同款爱心视频
【卡顿是录屏原因嘻嘻】
3 代码
源码需要安装 tkinter 库
可通过
pip install tkinter
命令安装
颜色选择器
class Chooser(Dialog):
command = "tk_chooseColor"
def _fixoptions(self):
"""Ensure initialcolor is a tk color string.
Convert initialcolor from a RGB triplet to a color string.
"""
try:
color = self.options["initialcolor"]
if isinstance(color, tuple):
# Assume an RGB triplet.
self.options["initialcolor"] = "#%02x%02x%02x" % color
except KeyError:
pass
def _fixresult(self, widget, result):
"""Adjust result returned from call to tk_chooseColor.
Return both an RGB tuple of ints in the range (0, 255) and the
tk color string in the form #rrggbb.
"""
# Result can be many things: an empty tuple, an empty string, or
# a _tkinter.Tcl_Obj, so this somewhat weird check handles that.
if not result or not str(result):
return None, None # canceled
# To simplify application code, the color chooser returns
# an RGB tuple together with the Tk color string.
r, g, b = widget.winfo_rgb(result)
return (r // 256, g // 256, b // 256), str(result)
## 方法二
class Application(Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.create_widgets()
def colorset(self):
setcolor = colorchooser.askcolor(color="red", title="背景色")
root.config(bg=setcolor[1])
def create_widgets(self):
self.button=Button(self,
text="choose",
command=self.colorset
)
self.button.pack()
UI交互界面
class Application(Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.font_size = 20
self.flag = True
# 主窗口
self.center_window(CANVAS_WIDTH, CANVAS_HEIGHT) # 窗口居中显示
def center_window(self, width, height):
screenwidth = root.winfo_screenwidth() # 获取显示屏宽度
screenheight = root.winfo_screenheight() # 获取显示屏高度
size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) /
2, (screenheight - height) / 2) # 设置窗口居中参数
root.geometry(size) # 让窗口居中显示
frame = Frame(root, width=300, height=400)
frame.pack()
frame.place(x=160, y=160)
Label(frame, text="表白对象:").grid(row=0)
Label(frame, text="爱心颜色:").grid(row=1)
self.name = Entry(frame)
self.name.grid(row=0, column=1, padx=10, pady=5)
# 颜色选择
def askcolor(color=None, **options):
"""Display dialog window for selection of a color.
Convenience wrapper for the Chooser class. Displays the color
chooser dialog with color as the initial value.
"""
global HEART_COLOR
if color:
options = options.copy()
options["initialcolor"] = color
choose_color = Chooser(**options).show()
HEART_COLOR = choose_color[1]
self.color_label['bg'] = HEART_COLOR # 颜色标签
print(HEART_COLOR)
return choose_color
self.color_label = Label(frame, height=1, width=2, bg=HEART_COLOR, justify=LEFT)
self.color_label.place(x=112, y=30) # 颜色卡
Button(frame, text="更改颜色", width=10, command=askcolor).place(x=140, y=25)
Button(frame, text="确定", width=10, command=self.dialog_window).grid(row=3, column=0, sticky="w", padx=10, pady=5)
Button(frame, text="退出", width=10, command=root.quit).grid(row=3, column=1, sticky="e", padx=10, pady=5)
# 显示爱心
def show_love(self):
canvas = Canvas(root, bg='black', height=CANVAS_HEIGHT, width=CANVAS_WIDTH)
canvas.pack()
heart = Heart()
self.draw(root, canvas, heart)
# 显示名字
def show_msg(self):
if self.font_size >= 14 and self.flag:
self.font_size = self.font_size - 2
if self.font_size == 14: self.flag = False
elif self.font_size <= 20 and not self.flag:
if self.font_size == 20: self.flag = True
self.font_size = self.font_size + 2
Label(root, text=self.name.get(), bg="black", fg=HEART_COLOR, font=("italic", self.font_size)).place(
relx=.5, rely=.5, anchor=CENTER)
# 弹出对话框
def dialog_window(self):
global isRun
isRun = tkinter.messagebox.askquestion(title='提示', message='是否运行该代码')
if isRun == 'yes':
self.show_love()
def draw(self, main: Tk, render_canvas: Canvas, render_heart: Heart, render_frame=0):
render_canvas.delete('all')
render_heart.render(render_canvas, render_frame)
main.after(160, self.draw, main, render_canvas, render_heart, render_frame + 1)
self.show_msg()
名字标签
名字标签随着心跳变化
这里频率好像没有对上,可以改进
# 显示名字
def show_msg(self):
if self.font_size >= 14 and self.flag:
self.font_size = self.font_size - 2
if self.font_size == 14: self.flag = False
elif self.font_size <= 20 and not self.flag:
if self.font_size == 20: self.flag = True
self.font_size = self.font_size + 2
Label(root, text=self.name.get(), bg="black", fg=HEART_COLOR, font=("italic", self.font_size)).place(
relx=.5, rely=.5, anchor=CENTER)
4 源码获取
关注公众号 [ 指针阿飞 ] 回复 [ 爱心 ] 免费领取
分享两个文件:
python爱心.exe
windows可直接执行,无需安装python环境love.py
是python源码,需要安装python环境、tkinter库
如果文章对你有帮助的话,欢迎评论
💬点赞
👍🏻 收藏
📂加关注