原文出自于传送门
欢迎访问博主的小破站传送门
博主使用python 制作了一个网上经常看见的搞笑表白程序
本来是想将其打包成exe程序的,但是因为里面涉及有图片
所以打包起来非常的麻烦,所以博主这里就没有打包了
其实刚开始博主想制作的并不是现在做出来的这个程序 而是和用js css做出来的那种一样
但是由于 这里我主要使用的是tkinter这个GUI库,就里面的许多操作实现起来就比较麻烦
最后博主就做的这种的了
说说做这个的时候遇到的一些问题吧!
-
在子页面加载图片问题
通过使用tkinter里面自带的加载图片工具是加载不了的
解决:通过 opencv里面的加载图片方法(下面的程序就是使用的这种方法)
通过 PIL里面的加载图片方法,但是要注意加载的图片类型
-
不允许关闭主窗口问题
这个程序 ,你发给小姐姐,结果人家直接一个钢叉给你关闭了,你这白忙活一宿🤣 就尴尬了
解决:
from Tkinter import * import tkMessageBox def callback(): if tkMessageBox.askokcancel("Quit", "Do you really wish to quit?"): root.destroy() root = Tk() root.protocol("WM_DELETE_WINDOW", callback) root.mainloop()
可以参考一下上面这段代码,或者去廖雪峰老师的网站看一下
博主使用的就和上面这个有一定的区别
# 让窗口不能关闭函数 def callback(event): # 查看触发事件的按钮 s=event.keysym if s == "Escape": root.destroy() root.protocol("WM_DELETE_WINDOW", callback) root.bind('<Key>',callback) root.focus_set()
-
布局问题
这里博主建议不会的小伙伴可以直接去看 这篇文章
博主这里就不说了,那边文章详细的介绍了布局
-
打包成exe程序问题
这个带图片的tkinterchengxu 打包成exe程序 有点太麻烦了,😥有操作比较流利的小伙伴可以教教博主、
解决:未解决
这里博主给一个百度网盘的分享链接传送门
提取码:2111
下面就直接给大家上源码
import tkinter as tk
from PIL import Image, ImageTk
import numpy as np
import cv2
import time
root = tk.Tk()
root.title("小姐姐,我来表白啦,别跑呀")
root.geometry("450x300")
root.resizable(0,0)
root.iconbitmap("icon.ico")
image=tk.PhotoImage(file="cp2.png")
# 不同意按钮弹窗
def disagree1():
root1 = tk.Tk()
root1.title("小姐姐,我来表白啦,别跑呀")
root1.geometry("300x200")
root1.resizable(0,0)
root1.iconbitmap("icon.ico")
l1= tk.Label(root1,text="我妈会游泳",font=('楷书',15))
l1.place(x=100,y=20)
btn= tk.Button(root1,text="勉强接受",bg="#e09780",command=root1.destroy)
btn.place(x=100,y=80)
btn1= tk.Button(root,text='勉强接受',bg="#AAAAFF",command=disagree2)
btn1.place(x=300,y=200)
root1.mainloop()
def disagree2():
# messagebox.showinfo('小姐姐我喜欢你','我妈会游泳')
root2 = tk.Tk()
root2.title("小姐姐,我来表白啦,别跑呀")
root2.geometry("300x200")
root2.resizable(0,0)
root2.iconbitmap("icon.ico")
l1= tk.Label(root2,text="保大",font=('楷书',25))
l1.place(x=100,y=20)
btn= tk.Button(root2,text="还不错",bg="#e09780",command=root2.destroy)
btn.place(x=100,y=80)
btn2= tk.Button(root,text='还不错',bg="#AAAAFF",command=disagree3)
btn2.place(x=300,y=200)
root2.mainloop()
def disagree3():
# messagebox.showinfo('小姐姐我喜欢你','我妈会游泳')
root3 = tk.Tk()
root3.title("小姐姐,我来表白啦,别跑呀")
root3.geometry("300x200")
root3.resizable(0,0)
root3.iconbitmap("icon.ico")
l1= tk.Label(root3,text="房产证写你的名字",font=('楷书',15))
l1.place(x=50,y=20)
btn= tk.Button(root3,text="还差点",bg="#e09780",command=root3.destroy)
btn.place(x=150,y=80)
btn2= tk.Button(root,text='还差点',bg="#AAAAFF",command=disagree4)
btn2.place(x=300,y=200)
root3.mainloop()
def disagree4():
# messagebox.showinfo('小姐姐我喜欢你','我妈会游泳')
root4 = tk.Tk()
root4.title("小姐姐,我来表白啦,别跑呀")
root4.geometry("300x200")
root4.resizable(0,0)
root4.iconbitmap("icon.ico")
l1= tk.Label(root4,text="给你买冰淇淋",font=('楷书',15))
l1.place(x=100,y=20)
btn= tk.Button(root4,text="这还差不多 哼",bg="#e09780",command=root4.destroy)
btn.place(x=150,y=80)
btn2= tk.Button(root,text='去你的',bg="#AAAAFF",command=disagree1)
btn2.place(x=300,y=200)
root4.mainloop()
# 同意按钮弹窗
def agree1():
root1 = tk.Tk()
root1.title("小姐姐,我来表白啦,别跑呀")
root1.geometry("300x200")
root1.resizable(0,0)
root1.iconbitmap("icon.ico")
l1= tk.Label(root1,text="我就知道你会同意的 嘻嘻",font=('楷书',10))
l1.place(x=100,y=20)
btn= tk.Button(root1,text="happy",bg="#e09780",command=agree2)
btn.place(x=150,y=80)
root1.mainloop()
def agree2():
root1 = tk.Tk()
root1.title("小姐姐,我来表白啦,别跑呀")
root1.geometry("300x200")
root1.resizable(0,0)
root1.iconbitmap("icon.ico")
l1= tk.Label(root1,text="走啦!我的小仙女 吃饭去",font=('楷书',15))
l1.place(x=20,y=20)
btn= tk.Button(root1,text="走咯",bg="#e09780",command=agree3)
btn.place(x=150,y=80)
root1.mainloop()
def agree3():
root1 = tk.Tk()
root1.title("小姐姐,我来表白啦,别跑呀")
root1.geometry("350x200")
root1.resizable(0,0)
root1.iconbitmap("icon.ico")
l1= tk.Label(root1,text="等等,我想牵着你的手手去 嘿嘿",font=('楷书',12))
l1.place(x=10,y=20)
btn= tk.Button(root1,text="走咯",bg="#e09780",command=agree4)
btn.place(x=150,y=80)
root1.mainloop()
# 显示素描图片
def agree4():
root1 = tk.Tk()
root1.title("小姐姐,我来表白啦,别跑呀")
root1.geometry("350x200")
root1.resizable(0,0)
root1.iconbitmap("icon.ico")
l1= tk.Label(root1,text='等等 给你个惊喜哦!')
l1.place(x=20,y=20)
btn= tk.Button(root1, text="点我哦",bg="#e09780",command=agree5)
btn.place(x=150,y=80)
root1.mainloop()
def agree5():
root1 = tk.Tk()
root1.title("小姐姐,我来表白啦,别跑呀")
root1.geometry("350x200")
root1.resizable(0,0)
root1.iconbitmap("icon.ico")
l1= tk.Label(root1,text='哈哈,骗你的小傻瓜!')
l1.place(x=20,y=20)
btn= tk.Button(root1, text="揍我?",bg="#e09780",command=agree6)
btn.place(x=150,y=80)
root1.mainloop()
def agree6():
root1 = tk.Tk()
root1.title("小姐姐,我来表白啦,别跑呀")
root1.geometry("350x200")
root1.resizable(0,0)
root1.iconbitmap("icon.ico")
l1= tk.Label(root1,text='我怎么舍得骗我的xxx呢')
l1.place(x=20,y=20)
btn= tk.Button(root1, text="有惊喜哦",bg="#e09780",command=agree7)
btn.place(x=150,y=80)
root1.mainloop()
def agree7():
root1 = tk.Tk()
root1.title("小姐姐,我来表白啦,别跑呀")
root1.geometry("350x200")
root1.resizable(0,0)
root1.iconbitmap("icon.ico")
l1= tk.Label(root1,text='嘿嘿 果然上当了 小笨蛋')
l1.place(x=20,y=20)
root1.bind('<Motion>',agree8)
root1.focus_set()
root1.mainloop()
def agree8(event):
root1 = tk.Tk()
root1.title("小姐姐,我来表白啦,别跑呀")
root1.geometry("350x200")
root1.resizable(0,0)
root1.iconbitmap("icon.ico")
l1= tk.Label(root1,text='当当当 别眨眼哦 惊喜在这呢')
l1.place(x=20,y=20)
root1.bind('<Motion>',agree9)
root1.focus_set()
root1.mainloop()
# 显示图片窗口
def agree9(event):
img = cv2.imread('image.jpg')
print(img)
cv2.imshow('image',img)
cv2.waitKey(5000)
cv2.destroyAllWindows()
# 这里是一个图片格式转换操作,如果使用PIL里面的方法显示图片的画有可能会出现格式错误打不开图片
# from PIL import Image
# img = Image.open("cp1.png")
# im= img.convert("RGB")
# im.save("D:\\File\\All\\python\\love\\image.jpg")
# 让窗口不能关闭函数
def callback(event):
# 查看触发事件的按钮
s=event.keysym
if s == "Escape":
root.destroy()
text3= tk.Label(root,text="小姐姐我观察你好久了")
text3.place(x=30,y=10)
text4= tk.Label(root,text="做我女朋友好不好?",font=('宋体',20))
text4.place(x=60,y=80)
btn1= tk.Button(root,text='我同意',bg="#AAAAFF",command=agree1)
btn1.place(x=100,y=200)
btn1= tk.Button(root,text='去你的',bg="#AAAAFF",command=disagree1)
btn1.place(x=300,y=200)
img= tk.Label(root, image=image)
img.place(x=300,y=40)
# 让x不能关闭窗口只能点escape才能关闭窗口
root.protocol("WM_DELETE_WINDOW", callback)
root.bind('<Key>',callback)
root.focus_set()
root.mainloop()
有问题的小伙伴可以直接私信博主(头像下面有联系方式哦)