import tkinter as tk
import random
import tkinter.messagebox as messagebox
root = tk.Tk( )
root.attributes( '-alpha' , 0 )
message = "做我女朋友好不好?"
screen_width = root.winfo_screenwidth( )
screen_height = root.winfo_screenheight( )
def create_popup( ) :popup = tk.Toplevel( root) popup.geometry( "300x150" ) popup.title( "表白弹框" ) popup.configure( bg= 'pink' ) x = random.randint( 0 , screen_width - 300 ) y = random.randint( 0 , screen_height - 150 ) popup.geometry( f"+{x}+{y}" ) canvas = tk.Canvas( popup, bg = 'pink' , highlightthickness = 0 ) canvas.pack( expand= True, fill = "both" ) label = tk.Label( canvas, text = message, font = ( "Helvetica" , 18 ) , bg = 'pink' , wraplength = 260 , justify = "center" ) canvas.create_window( 150 , 60 , window = label) button_frame = tk.Frame( canvas, bg = 'pink' ) canvas.create_window( 150 , 110 , window = button_frame) agree_button = tk.Button( button_frame, text = "同意" , command = on_agree) agree_button.pack( side= "left" , padx = 20 ) refuse_button = tk.Button( button_frame, text = "拒绝" , command = lambda: on_refuse( popup)) refuse_button.pack( side= "left" , padx = 20 ) popup.attributes( '-topmost' , True) popup.protocol( "WM_DELETE_WINDOW" , lambda: on_close( popup))
def on_agree( ) :messagebox.showinfo( "提示" , "我喜欢你" ) root.quit( )
def on_refuse( popup) :popup.destroy( ) create_popup( )
def on_close( popup) :popup.destroy( ) create_popup( )
create_popup( ) root.mainloop( )