-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.py
64 lines (49 loc) · 2.01 KB
/
2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import tkinter as tk
from PIL import Image, ImageTk
import random
import pygame
import threading
import requests
import os
# 画像と音声のURLを指定してください
image_url = 'https://github.com/Piliman22/1---/blob/main/sad.jpg?raw=true'
sound_url = 'https://github.com/Piliman22/1---/blob/main/sound.mp3?raw=true'
# ファイルをダウンロードする関数
def download_file(url, filename):
response = requests.get(url)
with open(filename, 'wb') as f:
f.write(response.content)
# 画像と音声をダウンロード
image_path = 'sad.jpg'
sound_path = 'sound.mp3'
download_file(image_url, image_path)
download_file(sound_url, sound_path)
def play_sound():
pygame.mixer.init()
pygame.mixer.music.load(sound_path)
pygame.mixer.music.play(-1) # ループ再生
def show_image():
window = tk.Toplevel() # 新しいウィンドウを作成
window.title("野獣先輩") # タイトルを設定
window.iconbitmap('sad.jpg') # アイコンを設定
img = Image.open(image_path)
img_tk = ImageTk.PhotoImage(img)
label = tk.Label(window, image=img_tk)
label.img_tk = img_tk # 画像をガベージコレクションから守るために参照を保持
label.pack()
# ランダムな位置にウィンドウを配置
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
window_width = img.width # 画像の幅を取得
window_height = img.height # 画像の高さを取得
random_x = random.randint(0, screen_width - window_width)
random_y = random.randint(0, screen_height - window_height)
window.geometry(f'{window_width}x{window_height}+{random_x}+{random_y}')
window.after(175, show_image)
# 音声再生を別スレッドで実行
sound_thread = threading.Thread(target=play_sound)
sound_thread.start()
root = tk.Tk()
root.withdraw() # メインウィンドウを隠す
show_image()
root.mainloop()