播放预览
剧集列表
黑洞风暴
import time
import tkinter as tk
from PIL import Image, ImageTk
import customtkinter as ctk
import random
import winsound
import threading
import ctypes
ctypes.windll.user32.SetProcessDPIAware()
# ------------ 路径配置(请换成你的本地图片和音频路径) ------------
IMAGE_PATH = r'C:\Users\tzq20\Desktop\ttt\raskrasil86.jpg'
AUDIO_PATH = r"C:\Users\tzq20\Desktop\ttt\我要掀起黑洞风暴.wav"
# ------------ customtkinter 配置 ------------
ctk.set_appearance_mode("dark") # "light" 或 "dark"
ctk.set_default_color_theme("blue") # 主题: "blue", "green", "dark-blue"
root = ctk.CTk()
root.title("赛罗黑洞风暴")
root.geometry('400x550')
#root.configure(bg="white")
# --- grid布局配置 ---
root.rowconfigure(0, weight=1) # 图片行自适应扩展
root.rowconfigure(1, weight=0) # 按钮行固定高度
root.columnconfigure(0, weight=1)
# ------------ 图片加载 ------------
original_img = Image.open(IMAGE_PATH)
# ------------ 图片Label控件(用tk.Label,便于兼容PIL图片) ------------
img_label = ctk.CTkLabel(root, text="", bg_color="white")
img_label.grid(row=0, column=0, sticky="nsew")
#img_label = tk.Label(root,bg="#202020")
#img_label.grid(row=0, column=0, sticky="nsew")
# ------------ 音频播放与气泡弹窗相关 ------------
is_playing = threading.Event()
def play_sound(wav):
winsound.PlaySound(wav, winsound.SND_FILENAME)
is_playing.clear()
def play():
if not is_playing.is_set():
threading.Thread(target=play_sound, args=(AUDIO_PATH,), daemon=True).start()
is_playing.set()
def show_bubble_after(left_times):
x = random.randint(0, 1800)
y = random.randint(0, 1200)
show_bubble(x, y)
if left_times > 1:
root.after(50, show_bubble_after, left_times - 1)
else:
print("已完成所有调用!")
def show_bubbles(count):
scaling = root.tk.call('tk', 'scaling')
play()
show_bubble_after(200)
def show_bubble(offsetX=0, offsetY=0):
# 计算气泡出现位置
x = offsetX
y = offsetY
# 创建Toplevel气泡窗口
bubble = tk.Toplevel(root)
bubble.overrideredirect(True)
bubble.attributes("-topmost", True)
bubble.wm_attributes("-transparentcolor", "#fef6d7")
b_width = 450
b_height = 100
bubble.geometry(f"{b_width}x{b_height}++{y}")
label = tk.Label(
bubble,
text="我是赛罗,我要掀起黑洞风暴",
bg="#fef6d7",
fg="white",
font=("微软雅黑", 25),
bd=5,
relief="ridge",
wraplength=450,
padx=8, pady=8,
)
label.pack(expand=True, fill="both")
bubble.after(random.randint(1000, 3000), bubble.destroy)
# ------------ 按钮控件 ------------
hello_button = ctk.CTkButton(
master=root,
text="点击我",
font=("微软雅黑",20),
command=lambda: show_bubbles(50),
corner_radius=20,
width=120,
height=40,
fg_color="#2CC985",
hover_color="#207A4D",
bg_color="white"
)
hello_button.grid(row=1, column=0, sticky="ew", padx=0, pady=0, ipady=15)
# ------------ 图片自适应缩放 ------------
def resize_img(event):
label_width = img_label.winfo_width()
label_height = img_label.winfo_height()
scaling = root.winfo_fpixels('1i') / 96
if label_width > 50 and label_height > 50:
scale = label_height / original_img.height
img_w = int(original_img.width * scale / scaling)
img_h = int(label_height / scaling)
#img_resized = original_img.resize((img_w, img_h), Image.BILINEAR)
#tk_img = ImageTk.PhotoImage(img_resized)
ctk_img = ctk.CTkImage(light_image=original_img, dark_image=original_img, size=(img_w, img_h))
img_label.configure(image = ctk_img)
img_label.image = ctk_img
img_label.bind('<Configure>', resize_img)
root.mainloop()
评论 (0)