How to write a confession program in python

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

How to write a confession program in python
How to write classes in python
How to write return in python
How to write win programs in python
Write a Qixi confession artifact in Python
How to write try statement in python
How to sort a dictionary in python
How to program based on interfaces in Python
How to simulate gravity in a Python game
python-Use python to write a small shopping program
Teach you how to write games in python
How to understand a list of numbers in python
How to create a Python virtual environment in Ubuntu 14.04
How to write python configuration file
How to wrap in python code
How to save the python program
How to filter numbers in python
How to read Excel in Python
How to view errors in python
How to understand variables in Python
How to clear variables in python
How to use SQLite in Python
How to make a globe with Python
How to use and and or in Python
How to delete cache files in python
How to introduce third-party modules in Python
How to save text files in python
How to run id function in python
How to install third-party modules in Python
How to custom catch errors in python
How to define private attributes in Python
How to add custom modules in Python
How to understand global variables in Python
How to debug python program using repr
How to open python in different systems
How to add background music in python
How to represent relative path in python
How to use the round function in python
How to use the zip function in Python
How to install python in ubuntu server environment
How to use the format function in python
How to use code running assistant in python
How to set code auto prompt in python
How to delete files and directories in python
How to install the downloaded module in python
Write gui in python
How to read and write files with Python
How to perform continuous multiplication calculation in python
How to create a CentOS virtual machine in VMware
How to understand the introduction of packages in Python
How to create a CentOS virtual machine in VMware
Example of how to automatically download pictures in python
How to save IE as an attachment in python
Python implements FTP to upload files in a loop
How to comment python code
How to write Pythonic code
How to uninstall python plugin
How to use python tuples
Do you still know how to draw cakes in python? ? ?
How to install Helm in Ubuntu
python how to view webpage code