use Tkinter Library creates a window object , Use condition judgment to control the content of the window and when to end the window process .
from tkinter import *
import tkinter.messagebox as messagebox
class Application(Frame): # Create a class
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack() # It is equivalent to packaging components into GUI in
self.createWidgets()
def createWidgets(self): # Set the contents of the main window
self.tiplabel = Label(self, text=' If I were DJ Will you love me ?')
self.tiplabel.pack()
self.valueInput = Entry(self) # Enter text box
self.valueInput.pack()
self.alertButton = Button(self, text=' verification ', command=self.proof) # Submit button
self.alertButton.pack()
def proof(self): # Set the logic and content of the answer window
keyvalue = self.valueInput.get()
if keyvalue == ' Meeting ':
messagebox.showinfo(' Little secret ', ' I love you too ~')
root.destroy()
else:
messagebox.showerror(' Wrong answer ', ' I think you can think about it again ?')
def callback(): # If the user clicks close window
messagebox.showwarning(' Warning ',' Please don't avoid my love for you ')
root = Tk() # establish tkinter object
root.geometry('300x150') # Set window size
app = Application().pack() # Create a custom class object
root.protocol("WM_DELETE_WINDOW", callback) # If the user closes the window, execute callback function
root.mainloop() # Make the window process cycle
This code embodies the object-oriented program (OOP). It is Python One of the most powerful features , For details, please refer to This one knows The content of .
When building windows , We have done this to prevent users from closing windows directly . Through other logical decisions , We can even make the upper left corner of the window on the user's mouse 50 Within pixels “ flash ” To other places . Interested students can think about how to achieve .( It's better to confess directly ?)