
import time
import tkinter as tk
import random
#打包程序
#pyinstaller --onefile --noconsole 你的python文件名.py
def move_button_away(event):
"""当鼠标接近按钮时,将按钮移动到一个随机位置。"""
button_width = button1.winfo_width()
button_height = button1.winfo_height()
screen_width = root.winfo_width()
screen_height = root.winfo_height()
# 窗口的偏移位置
offset_x = root.winfo_x()
offset_y = root.winfo_y()
# 计算按钮的新位置
new_x = random.randint(0, screen_width - button_width)
new_y = random.randint(0, screen_height - button_height)
# 更新按钮的位置
button1.place(x=new_x, y=new_y)
def xiang():
result.config(text="你做梦")
def buxiang():
result.config(text="果然你是一个爱学习的好孩子")
# 创建主窗口
root = tk.Tk()
root.title("是否想要放假?")
root.geometry("600x400")
question=tk.Label(root, text="是否想要放假?", font=("华文行楷", 16))
question.place(x=200, y=50)
# 创建一个按钮,并绑定鼠标接近事件
button1= tk.Button(root, text="想", font=("Arial", 16),command=xiang)
button1.place(x=250, y=150) # 初始位置
button1.bind("<Enter>", move_button_away) # 鼠标进入按钮时触发移动
button2= tk.Button(root, text="不想", font=("Arial", 16),command=buxiang)
button2.place(x=250, y=200)
result = tk.Label(root,text="",font=("Arial", 16))
result.place(x=150,y=250)
# 运行主循环
root.mainloop()