|
12 | 12 | import tkinter as tk |
13 | 13 | import os |
14 | 14 | import time |
| 15 | +from io import BytesIO |
15 | 16 | from PIL import Image, ImageDraw, ImageFont, ImageTk |
16 | | -import _thread as thread |
17 | 17 | from threading import Thread,Event |
18 | 18 | path=os.path.dirname(__file__) |
19 | 19 | pathjoin=os.path.join |
|
26 | 26 | RETURN_PHOTOIMAGE=0 |
27 | 27 | RETURN_IMAGE=1 |
28 | 28 | RETURN_BYTE=2 |
| 29 | +RETURN_SAVETOFILE=3 |
29 | 30 |
|
30 | | -def generate_image(toast:str, image_path:str, text1:str, color1:str, text2:str, color2:str, return_mode=RETURN_IMAGE): |
| 31 | +def generate_image(toast=ADVANCEMENT, image_path:str=None, text1="进度已达成!", color1="yellow", text2="MCToast示例", color2="white", return_mode=RETURN_IMAGE, resize:bool=False, filename:str=None): |
31 | 32 | """生成Toast图片 |
32 | 33 | toast:str 背景图片(ADVANCEMENT,RECIPE,SYSTEM) |
33 | 34 | image_path:str 图片路径(对应原版的物品位置) |
34 | 35 | text1:str 第一行文本 |
35 | 36 | color1:str 第一行文本颜色 |
36 | 37 | text2:str 第二行文本 |
37 | 38 | color2:str 第二行文本颜色 |
38 | | - return_mode:int 返回模式(RETURN_IMAGE,RETURN_PHOTOIMAGE,RETURN_BYTE) |
| 39 | + return_mode:int 返回模式(RETURN_IMAGE,RETURN_PHOTOIMAGE,RETURN_BYTE,RETURN_SAVETOFILE) |
| 40 | + filename:str 在return_mode=RETURN_SAVETOFILE时作为保存路径,未指定就报错 |
39 | 41 | """ |
40 | 42 | # 打开背景图片并缩放 |
41 | 43 | background_image = Image.open(toast) |
@@ -67,14 +69,23 @@ def generate_image(toast:str, image_path:str, text1:str, color1:str, text2:str, |
67 | 69 | draw.text((120, 26), text1, fill=color1, font=font) |
68 | 70 | if text2 and color2: |
69 | 71 | draw.text((120, 70), text2, fill=color2, font=font) |
70 | | - |
| 72 | + if resize: |
| 73 | + background_image = background_image.resize((320, 64),Image.Resampling.NEAREST) |
71 | 74 | # 将 Pillow 图片转换为 PhotoImage |
72 | 75 | if return_mode==RETURN_IMAGE: |
73 | | - return background_image.resize((320, 64),Image.Resampling.NEAREST) |
| 76 | + return background_image |
74 | 77 | elif return_mode==RETURN_BYTE: |
75 | | - return background_image.resize((320, 64),Image.Resampling.NEAREST).tobytes() |
| 78 | + bytes=BytesIO() |
| 79 | + background_image.save(bytes,format="PNG") |
| 80 | + return bytes.getvalue() |
| 81 | + elif return_mode==RETURN_SAVETOFILE: |
| 82 | + if filename: |
| 83 | + background_image.save(filename) |
| 84 | + else: |
| 85 | + raise ValueError("未指定图片路径") |
| 86 | + |
76 | 87 | else: |
77 | | - return ImageTk.PhotoImage(background_image.resize((320, 64),Image.Resampling.NEAREST)) |
| 88 | + return ImageTk.PhotoImage(background_image) |
78 | 89 |
|
79 | 90 | class ToastWindowUI: |
80 | 91 | """Toast界面类""" |
@@ -108,7 +119,7 @@ def new_toast(self, toast=ADVANCEMENT, image_path=None, text1="一个弹窗", co |
108 | 119 | for i in range(5): |
109 | 120 | if toasts[i] == None: |
110 | 121 | # 使用 Pillow 生成图片 |
111 | | - photo=generate_image(toast, image_path, text1, color1, text2, color2, RETURN_PHOTOIMAGE) |
| 122 | + photo=generate_image(toast, image_path, text1, color1, text2, color2, RETURN_PHOTOIMAGE, True) |
112 | 123 | self.root.deiconify() |
113 | 124 | toasts[i] = self.canvas.create_image(320, i*64, anchor="nw", image=photo) |
114 | 125 | event.set() |
@@ -163,17 +174,20 @@ def set_no_focus(self): |
163 | 174 | self.root.wm_attributes('-type', 'splash') |
164 | 175 |
|
165 | 176 | window=None |
166 | | -def _init(): |
| 177 | +def _init(e:Event=None): |
167 | 178 | """别调用""" |
168 | 179 | global window |
169 | 180 | window=ToastWindowUI() |
| 181 | + if e: |
| 182 | + e.set() |
170 | 183 | window.main() |
171 | 184 |
|
172 | 185 | def init(): |
173 | 186 | """初始化窗口""" |
174 | | - thread.start_new_thread(_init,()) |
175 | | - while type(window)!=ToastWindowUI: |
176 | | - pass |
| 187 | + e=Event() |
| 188 | + t=Thread(target=_init,args=(e)) |
| 189 | + t.start() |
| 190 | + e.wait() |
177 | 191 |
|
178 | 192 | def new_toast(toast=ADVANCEMENT, image_path:str=None, text1="一个弹窗", color1="yellow", text2="MCToast示例", color2="white"): |
179 | 193 | """新弹窗 |
|
0 commit comments