# Python3中tkinte制作表白小程序

Python3中tkinter简单小程序

全程注释,方便理解

from PIL import Image ,ImageTk
#下载包 或pycharm中安装pillow
图像处理库,,强大的图像处理能力,主要包括图像储存、图像显示、格式转换以及基本的图像处理操作

from tkinter import *
#下载包 或pycharm中安装Easy thinter

import tkinter
#简单的图形开发界面的库

import tkinter.messagebox
#对话框模块,弹出对话框框

敲黑板

自己修改一下,我这个是注定单生版的,请自行修改以下两句,哈哈!

    tkinter.messagebox.showinfo( title="欧耶", message="我不同意咯!")
    tkinter.messagebox.showinfo( title="欧耶", message="别再打扰我了!")
from tkinter import *                   #下载包 或pycharm中安装Easy thinter
from PIL import Image ,ImageTk          #下载包 或pycharm中安装pillow
import tkinter                          #简单的图形开发界面的库
import tkinter.messagebox               #对话框模块,弹出对话框框

root=Tk()                               #创建一个主窗口
root.title('小姐姐,我要向你表白!')            #窗口名字
root.geometry("600x500+300+100")    #窗口的尺寸和位置
cv= Canvas(root, width = 600, height =500, bg = "white")    #画布控件尺寸 和背景色
                                    #Canvas用于绘制图形,创建图形编辑器以及实现自定制的小构件类
cv.pack()                           #显示cv里面的内容

a = os.path.abspath('tupian.jpg') #获取当前目录下的 名为“tupian.jpg”的文件,图片名字随便取但是要对应修改哈
image = Image.open(a)       #打开图片
im = ImageTk.PhotoImage(image)				#创建一个Tkinter兼容的照片图像(photo image),
											#它可在Tkinter期望一个图像对象的任何地方使用。
cv.create_image(270,190,image = im)    #设置边框大小,并以图片作为窗口的背景

def oppose():
    tkinter.messagebox.showinfo(title='扎心了!', message='再考虑考虑吧,不要急着回答')
                                        #tkinter.messagebox.showinfo弹窗显示文本消息
                                         #title为窗口标题 ,message为消息;

def agree():
    tkinter.messagebox.showinfo( title="欧耶", message="我不同意咯!")
    tkinter.messagebox.showinfo( title="欧耶", message="别再打扰我了!")
    root.destroy()

def hesitate ():
    tkinter.messagebox.showinfo(title='别纠结了', message='你完了,你妈让你嫁给我')
    tkinter.messagebox.showinfo(title='别纠结了', message='你爸也是这么说的')
    tkinter.messagebox.showinfo(title='别纠结了', message='你奶奶也让你嫁给我')
    tkinter.messagebox.showinfo(title='别纠结了', message='你哥哥也同意了')
    tkinter.messagebox.showinfo(title='别纠结了', message='你闺蜜说嫁给我没错')
    tkinter.messagebox.showinfo(title='别纠结了', message='你爸说不同意就打你')
    tkinter.messagebox.showinfo(title='别纠结了', message='掉水我先救你')
    tkinter.messagebox.showinfo(title='别纠结了', message='孩子难产我保大')
    tkinter.messagebox.showinfo(title='别纠结了', message='银行卡都给你')
    tkinter.messagebox.showinfo(title='别纠结了', message='接受现实吧,我会对你好的')

def closeWindow():
    tkinter.messagebox.showerror(title='未作回应',message='小姐姐,请不要逃避!')
    return  False


root.protocol('WM_DELETE_WINDOW', closeWindow)

output_label0 = Label(root,text ="实话告诉你吧", font =("仿宋", 16,"bold"))
cv.create_window(20, 20, anchor=NW, window= output_label0)   #anchor对齐方式 NW西北 ,
                                                            #window= output_label0 给窗口绑定文本output_label0
                                                            #create_window创建坐标为x1,y1,x2,y2的窗口。
                                                             #"bold"字体样式 
output_label1 = Label(root,text ="我喜欢你很久了", font =("仿宋", 16,"bold"))
cv.create_window(40, 70, anchor=NW, window= output_label1)
output_label2 = Label(root,text ="你看着办吧", font =("仿宋", 16,"bold"))
cv.create_window(50, 110, anchor=NW, window= output_label2)
Button1 = Button(root, text="同意", font =("仿宋", 16,"bold"),width=10,height=1,command = agree)
cv.create_window(110, 430, anchor=NW, window=Button1)       #command = showDialogOK 点击执行函数showDialogOK()
Button2 = Button(root,text='考虑一下', font =("仿宋", 16,"bold"),width=10,height=1,command = hesitate )
cv.create_window(260, 430, anchor=NW, window=Button2)       #command = showDialogEE 点击执行函数showDialogEE()
Button3 = Button(root,text='不同意', font =("仿宋", 16,"bold"),width=10,height=1,command = oppose)
cv.create_window(450, 430, anchor=NW, window=Button3)       #command = showDialogNO 点击执行函数sshowDialogNO()
root.mainloop()       #创建事件循环直到关闭主窗口root

在这里插入图片描述

打包exe文件

请将注释去掉,不然会报错,如下
请将上图和生成的exe放在一个文件下,方便exe查找图片,后面将文件夹压缩就可以发送了。
文中 a = os.path.abspath(‘tupian.jpg’) #这里写你的图片的名字 ,自动获取exe文件当前路径并加上图片名生成图片id

#-*- coding:utf-8 -*-
from tkinter import *
from PIL import Image ,ImageTk
import tkinter
import tkinter.messagebox
import os

root=Tk()
root.title('小姐姐,我要向你表白!')
root.geometry("600x500+300+100")
cv= Canvas(root, width = 600, height =500, bg = "white")
cv.pack()

a = os.path.abspath('tupian.jpg')
image = Image.open(a)
im = ImageTk.PhotoImage(image)

cv.create_image(270,190,image = im)
def oppose():
    tkinter.messagebox.showinfo(title='扎心了!', message='再考虑考虑吧,不要急着回答')

def agree():
    tkinter.messagebox.showinfo( title="欧耶", message="我不同意咯!")
    tkinter.messagebox.showinfo( title="欧耶", message="别再打扰我了!")
    root.destroy()

def hesitate ():
    tkinter.messagebox.showinfo(title='别纠结了', message='你完了,你妈让你嫁给我')
    tkinter.messagebox.showinfo(title='别纠结了', message='你爸也是这么说的')
    tkinter.messagebox.showinfo(title='别纠结了', message='你奶奶也让你嫁给我')
    tkinter.messagebox.showinfo(title='别纠结了', message='你哥哥也同意了')
    tkinter.messagebox.showinfo(title='别纠结了', message='你闺蜜说嫁给我没错')
    tkinter.messagebox.showinfo(title='别纠结了', message='你爸说不同意就打你')
    tkinter.messagebox.showinfo(title='别纠结了', message='掉水我先救你')
    tkinter.messagebox.showinfo(title='别纠结了', message='孩子难产我保大')
    tkinter.messagebox.showinfo(title='别纠结了', message='银行卡都给你')
    tkinter.messagebox.showinfo(title='别纠结了', message='接受现实吧,我会对你好的')

def closeWindow():
    tkinter.messagebox.showerror(title='未作回应',message='小姐姐,请不要逃避!')
    return  False


root.protocol('WM_DELETE_WINDOW', closeWindow)
output_label0 = Label(root,text ="实话告诉你吧", font =("仿宋", 16,"bold"))
cv.create_window(20, 20, anchor=NW, window= output_label0)
output_label1 = Label(root,text ="我喜欢你很久了", font =("仿宋", 16,"bold"))
cv.create_window(40, 70, anchor=NW, window= output_label1)
output_label2 = Label(root,text ="你看着办吧", font =("仿宋", 16,"bold"))
cv.create_window(50, 110, anchor=NW, window= output_label2)
Button1 = Button(root, text="同意", font =("仿宋", 16,"bold"),width=10,height=1,command = agree)
cv.create_window(110, 430, anchor=NW, window=Button1)
Button2 = Button(root,text='考虑一下', font =("仿宋", 16,"bold"),width=10,height=1,command = hesitate )
cv.create_window(260, 430, anchor=NW, window=Button2)
Button3 = Button(root,text='不同意', font =("仿宋", 16,"bold"),width=10,height=1,command = oppose)
cv.create_window(450, 430, anchor=NW, window=Button3)
root.mainloop()

这里im = ImageTk.PhotoImage(image)这一句下文解释
原文解释参考:https://blog.csdn.net/liubing8609/article/details/78399267

PhotoImage类
ImageTk.PhotoImage(image) ⇒ PhotoImage instance
创建一个Tkinter兼容的照片图像(photo image),它可在Tkinter期望一个图像对象的任何地方使用。

如果图像是一个RGBA图像,那么具有alpha 0的像素就被视为透明的。

ImageTk.PhotoImage(mode, size) ⇒ PhotoImage instance
与上面一样,但是创建一个空的(透明的)照片图像(photo image)。

使用粘贴复制图像数据到这个对象。
解释参考:https://blog.csdn.net/liubing8609/article/details/78399267 

猜你喜欢

转载自blog.csdn.net/weixin_43097301/article/details/84261960