程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

A window that cannot be closed -- express his unstoppable love for TA in his heart with Python

編輯:Python

Python Window control

Realization principle

use Tkinter Library creates a window object , Use condition judgment to control the content of the window and when to end the window process .

Code

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 

Functional expansion

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 ?)


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved