I believe everyone has seen that kind of expression artifact on the Internet , You can't close the window if you don't agree , Such technology is particularly advanced in Xiaobai's eyes , But as technicians, we , Such a thing is not a piece of cake , There's no one to tell us. We can't tell ourselves

<> Pre preparation

The programming language we use here is Python, The library used is tkinter, There are other modules such as requests etc.

The third party libraries are requests,PIL

Installation method
pip install Library name
in addition , Let's pack exe Documents need to be used pyinstaller, The installation method is the same pip

<> Window design

Let's make a nice window first , Only in this way can we win the heart of the goddess

The key codes are as follows
from tkinter import * from tkinter import messagebox from requests import get
from PIL import Image,ImageTk import sys class Window(object): def __init__(self
): self.window=Tk() self.title=" A little brother who loves you " self.size=(500,340) self.image=Image.
open('./love.jpg') def show(self): self.window.geometry("%dx%d"%self.size) self.
window.title(self.title) label1=Label(self.window,text=" cute girl , I have been in love with for a long time. ",font=(
" Microsoft YaHei ",15)).place(x=0,y=0) tk_image=ImageTk.PhotoImage(self.image) img_label=
Label(self.window,image=tk_image).place(x=0,y=40) label2=Label(self.window,text=
" Do you agree with me ?",font=(' Microsoft YaHei ',30),fg='red').place(x=220,y=150) agree_b=Button(self.
window,text=" agree! ",bg="#4e6ef2",fg="#fff",font=(' Microsoft YaHei ',20),command=lambda :self.
agree()) agree_b.place(x=40,y=260) disagree_b=Button(self.window,text=" disagree ",bg=
"#4e6ef2",fg="#fff",font=(' Microsoft YaHei ',20),command=lambda :self.disagree()) disagree_b
.place(x=270,y=260) self.window.mainloop() def agree(self): pass def disagree(
self): pass def try_to_close_window(self): pass if __name__ == '__main__':
window=Window() window.show()
I also have a picture here , I got the picture from the Internet , And then the scale is equal , Self preservation

In that case , We have such a successful interface

<> event listeners

When we press disagree , A dialog box is displayed , When we press the close button , A dialog box is displayed , When we press agree , Will press a dialog box to exit the program , So how did we do that

display a dialog box
We passed tkinter Modular messagebox To display the dialog box
messagebox.showinfo( title , content )
Then we modify the code as follows
from tkinter import * from tkinter import messagebox from requests import get
from PIL import Image,ImageTk import sys class Window(object): def __init__(self
): self.window=Tk() self.title=" A little brother who loves you " self.size=(500,340) self.image=Image.
open('./love.jpg') def show(self): self.window.geometry("%dx%d"%self.size) self.
window.title(self.title) label1=Label(self.window,text=" cute girl , I have been in love with for a long time. ",font=(
" Microsoft YaHei ",15)).place(x=0,y=0) tk_image=ImageTk.PhotoImage(self.image) img_label=
Label(self.window,image=tk_image).place(x=0,y=40) label2=Label(self.window,text=
" Do you agree with me ?",font=(' Microsoft YaHei ',30),fg='red').place(x=220,y=150) agree_b=Button(self.
window,text=" agree! ",bg="#4e6ef2",fg="#fff",font=(' Microsoft YaHei ',20),command=lambda :self.
agree()) agree_b.place(x=40,y=260) disagree_b=Button(self.window,text=" disagree ",bg=
"#4e6ef2",fg="#fff",font=(' Microsoft YaHei ',20),command=lambda :self.disagree()) disagree_b
.place(x=270,y=260) self.window.mainloop() def agree(self): messagebox.showinfo(
""," I love you too. , kiss you ") sys.exit() def disagree(self): messagebox.showinfo(""," You can't disagree ")
def try_to_close_window(self): pass if __name__ == '__main__': window=Window()
window.show()
Window close button monitoring
When the close button is pressed , We'll pop up a dialog , So how do we monitor the close button ?

We can go through it Tk Object protocol Method
Tk.protocol("WM_DELETE_WINDOW",function)
among function It's the function we're going to execute when we press the button

The complete code is as follows
from tkinter import * from tkinter import messagebox from requests import get
from PIL import Image,ImageTk import sys class Window(object): def __init__(self
): self.window=Tk() self.title=" A little brother who loves you " self.size=(500,340) self.image=Image.
open('./love.jpg') def show(self): self.window.geometry("%dx%d"%self.size) self.
window.title(self.title) label1=Label(self.window,text=" cute girl , I have been in love with for a long time. ",font=(
" Microsoft YaHei ",15)).place(x=0,y=0) tk_image=ImageTk.PhotoImage(self.image) img_label=
Label(self.window,image=tk_image).place(x=0,y=40) label2=Label(self.window,text=
" Do you agree with me ?",font=(' Microsoft YaHei ',30),fg='red').place(x=220,y=150) agree_b=Button(self.
window,text=" agree! ",bg="#4e6ef2",fg="#fff",font=(' Microsoft YaHei ',20),command=lambda :self.
agree()) agree_b.place(x=40,y=260) disagree_b=Button(self.window,text=" disagree ",bg=
"#4e6ef2",fg="#fff",font=(' Microsoft YaHei ',20),command=lambda :self.disagree()) disagree_b
.place(x=270,y=260) self.window.protocol("WM_DELETE_WINDOW",lambda :self.
try_to_close_window()) self.window.mainloop() def agree(self): messagebox.
showinfo(""," I love you too. , kiss you ") sys.exit() def disagree(self): messagebox.showinfo("",
" You can't disagree ") def try_to_close_window(self): messagebox.showinfo("",' You can't close the window if you don't agree ')
if __name__ == '__main__': window=Window() window.show()
<> pack exe

We open it dos,cd To file directory , Then type in
pyinstaller -F file name
For example, I named my file love.py, I'll type it pyinstaller -F love.py
This is what happens when the package is successful

At this time, we will appear in the same directory __pycache__,dist,build Three folders , stay dist That's what we want exe The document is ready , The other two folders can be deleted

Then you can send the document to the goddess , Remember to bring the picture with you when you send it

Technology