python表白程序,无法拒绝

python表白程序,无法拒绝_第1张图片

# coding=gbk
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
    canvas = tk.Canvas(popup, bg='pink', highlightthickness=0)
    canvas.pack(expand=True, fill="both")

    # 创建Label并设置字体大小以铺满Canvas
    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()




你可能感兴趣的:(python,开发语言)