The method of compiling a confession program with python:
1、 Create a GUI window to implement code calls.
2、 Write the click trigger function to realize the confession program.
The specific code is as follows:
from tkinter import*from tkinter import messagebox
def closeWindow():
messagebox.showinfo(title="caveat",message="Don't close, answer well")
# messagebox.showerror(title="caveat",message="Don't close, answer well")return
# Click like to trigger method
def Love():
# Top-level window
love =Toplevel(window)
love.geometry("300x100+520+260")
love.title("What a coincidence, me too")
label =Label(love,text ="What a coincidence, me too",font =("Microsoft Yahei",20))
label.pack()
btn =Button(love,text="determine",width=10,height=2,command=closeAllWindow)
btn.pack()
love.protocol("WM_DELETE_WINDOW",closeLove)
def closeLove():return
# Close all windows
def closeAllWindow():
# destroy
window.destroy()
def noLove():
no_love =Toplevel(window)
no_love.geometry("300x100+520+260")
no_love.title("Consider again")
label =Label(no_love,text="Consider again",font=("Microsoft Yahei",25))
label.pack()
btn =Button(no_love,text="Ok",width=10,height=2,command=no_love.destroy)
btn.pack()
no_love.protocol("WM_DELETE_WINDOW",closeNoLove)
def closeNoLove():noLove()
# Create parent window
window =Tk() #Tk is a class
# Window title
window.title("Do you like me?")
# Window size
window.geometry('380x420')
# Window position geometry:geometry
window.geometry('+500+240')
# protocol()Event triggered by user closing window
window.protocol("WM_DELETE_WINDOW",closeWindow)
# Label control
label =Label(window,text="hey,Little sister",font=("Microsoft Yahei",15),fg='red')
# Positioning grid layout pack is also available
label.grid(row =0,column =0,sticky = W)
label1 =Label(window,text="like me?",font=("Microsoft Yahei",30))
# sticky alignment EWSN east west south north
label1.grid(row =1,column =1,sticky = E)
# display image
photo =PhotoImage(file ="./cc.png")
imageLable =Label(window,image = photo)
# The number of columns spanned by the columnspan component
imageLable.grid(row =2, columnspan =2)
# Button control
# The event triggered by the command button click
btn =Button(window,text="like",width=15,height=2,command=Love)
btn.grid(row =3,column =0, sticky = W)
btn1 =Button(window,text="dislike",command=noLove)
btn1.grid(row =3,column =1, sticky = E)
# Display window, also called message loop
window.mainloop()
Example extension:
copy#Packing operation
# Install pyinstaller
# cmd enter pip install pyinstaller
# shift+Right click on the folder and click here to open the command window
# pyinstaller -F -w love.py //Packager
# Reference tkinter toolkit
from tkinter import* #__all__=[a,b]
# from tkinter import messagebox
# Define close window prompt
def closeWindow():
messagebox.showinfo(title="caveat",message ="Roll back, you must choose one")return
# Define the reminder to like the button
def Love():
love =Toplevel(window)
love.geometry("300x100+250+260")
love.title("I like you too")
label =Label(love, text ="I like you too!",font =("Microsoft Yahei",20))
label.pack()
btn =Button(love,text ="OK",width =10,height =2,command=closeallwindow)
btn.pack()
love.protocol("WM_DELETE_WINDOW", closelove)
def closelove():return
# Close all windows
def closeallwindow():
window.destroy()
# Define the prompt for dislike button
def noLove():
no_love =Toplevel(window)
no_love.geometry("300x100+520+260")
no_love.title("Reselect")
label =Label(no_love,text="Go back and choose again!",font =("Microsoft Yahei",25))
label.pack()
btn =Button(no_love,text="OK", width=10, height=2,command=no_love.destroy)
btn.pack()
no_love.protocol("WM_DELETE_WINDOW",closenolove)
# Window cannot be closed
def closenolove():
# messagebox.showinfo("Give up, go back and re-elect- -")
# returnnoLove()
# Create window
window =Tk()
# Window title
window.title("like me?")
# Window size
window.geometry("380x420+500+240")
# love.title("Haha")
btn=Button(text="determine")
# Window position
# window.geometry("+500+240")
window.protocol("WM_DELETE_WINDOW",closeWindow )
# Label control
label =Label(window,text ="hey,Little sister",font =("Microsoft Yahei",15),fg ="red")
label.grid(row =0, column =0)
label =Label(window,text ="like me?",font =("Microsoft Yahei",30))
label.grid(row =1, column =1,sticky = E)
# Insert picture
photo =PhotoImage(file ="./123.png")
imageLable =Label(window,image = photo)
imageLable.grid(row =2,columnspan =2)
# Like button plugin
btn =Button(window,text="like",width=15,height=2,command=Love)
btn.grid(row=3,column=0,sticky= W)
# Don't like button plugins
btn =Button(window,text="dislike",command=noLove)
btn.grid(row=3,column=1,sticky= E)
# Display window message loop
window.mainloop()
So far, this article on how to write a confession program in python is introduced. For more information about how to use python to write a confession program, please search ZaLou.Cn
Recommended Posts