# This is a sample Python script.
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
import math
import threading
import time
import tkinter as tk
import pygame
from PIL import Image, ImageTk
import random
# 初始化Tkinter視窗
root = tk.Tk()
root.title("抽獎程式")
# 設定視窗大小
root.geometry("1400x700")
# 載入相片
photo_paths = [f"C:/photos/{i}.png" for i in range(1, 6)]
photos = [Image.open(path).resize((256, 256)) for path in photo_paths]
photo_tk = [ImageTk.PhotoImage(photo) for photo in photos]
# 設定音效
sound_path = "C:/sounds/CSGO Case Knife Opening Sound Effect.mp3"
pygame.mixer.init()
sound = pygame.mixer.Sound(sound_path)
# 計算每張圖片的寬度
image_width = photo_tk[0].width()
# 建立Canvas來放置Label,足夠寬以容納所有五張圖片
canvas = tk.Canvas(root, width=image_width * 5, height=500)
# 顯示相片的Label
photo_labels = []
for i in range(5):
label = canvas.create_image(i * image_width, 100, anchor='nw', image=photo_tk[i])
photo_labels.append(label)
canvas.pack()
# 加入紅線到正中間的圖片的左側框和右側框
middle_image_index = 2 # 假設第三張圖片是中間的圖片
middle_image_width = image_width
middle_image_height = photo_tk[middle_image_index].height()
red_line_width = 3 # 紅線的寬度
# 計算紅線的位置
x1_left = middle_image_index * middle_image_width # 左側框的 x1 座標
y1 = 100
x2_left = x1_left + red_line_width # 左側框的 x2 座標
x1_right = x2_left + middle_image_width # 右側框的 x1 座標
x2_right = x1_right + red_line_width # 右側框的 x2 座標
# 繪製紅線
red_line_up = canvas.create_rectangle(x1_left, y1, x2_right, middle_image_height + y1, outline="red", width=red_line_width)
positions = [pos * middle_image_width for pos in range(5)]
confirm_button = tk.Button(root, text="確認")
# 開始抽選
def perform_lottery():
sound.play()
time.sleep(2)
# 第一段移動,0.001秒的速度,2000次
move_photo(0.01, 100 + random.randint(1, 5))
# 第二段移動,0.01秒的速度,200次
move_photo(0.1, 30)
# 第三段移動,1秒的速度,2次
move_photo(0.5, 1)
# 停在得獎者位置
# winner_index = random.randint(0, 4)
# winner_labels = []
# for i in range(5):
# winner_labels = canvas.create_image(((i+2) % 5) * image_width, 0, anchor='nw',
# image=photo_tk[(winner_index + i) % 5])
# canvas.create_rectangle(x1_left, y1, x2_left, middle_image_height, outline="red",
# width=red_line_width)
# canvas.create_rectangle(x1_right, y1, x2_right, middle_image_height, outline="red",
# width=red_line_width)
# photo_labels.append(winner_labels)
# 抽選結束
def move_photo(delay, iterations):
for _ in range(iterations):
time.sleep(delay)
for i in range(5):
new_x = positions[(i + 1) % 5]
canvas.coords(photo_labels[i], new_x, 100)
canvas.update_idletasks()
temp = positions[0]
for i in range(4):
positions[i] = positions[i + 1]
positions[4] = temp
# 按下確認按鈕開始抽選
confirm_button.config(command=perform_lottery, width=20, height=5)
confirm_button.pack()
# 顯示視窗
root.mainloop()
# See PyCharm help at https://www.jetbrains.com/help/pycharm/