Skip to content

Commit 0029250

Browse files
committed
1.10
1 parent da289ee commit 0029250

File tree

4 files changed

+147
-14
lines changed

4 files changed

+147
-14
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ mctoast模仿的效果:
2626

2727
## 更新日志
2828

29+
### 1.10
30+
- 库的修改
31+
    - 为`generate_image`添加了默认值
32+
    - `generate_image(return_mode=RETURN_BYTE)`修复,现在返回的就是正常的图片字节
33+
    - 加入`generate_image(return_mode=RETURN_SAVETIFILE,filename="awasome.png")`语法,可以将图片保存为文件了
34+
    - 加入`generate_image(resize=False)`,在new_toast里使用的时候这个值为`True`,你一般不用修改,除非你也要把它缩放到320x64
35+
- 允许你使用`python -m mctoast`生成toast图片或弹出toast
36+
- <p style="color:gray">据说执行 python -m mctoast --moo 有彩蛋,你要不要试试</p>
37+
- 移除了Herobrine (
38+
2939
### 1.01
3040
- 修复:进度图片显示不正常
3141

mctoast/__init__.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
import tkinter as tk
1313
import os
1414
import time
15+
from io import BytesIO
1516
from PIL import Image, ImageDraw, ImageFont, ImageTk
16-
import _thread as thread
1717
from threading import Thread,Event
1818
path=os.path.dirname(__file__)
1919
pathjoin=os.path.join
@@ -26,16 +26,18 @@
2626
RETURN_PHOTOIMAGE=0
2727
RETURN_IMAGE=1
2828
RETURN_BYTE=2
29+
RETURN_SAVETOFILE=3
2930

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):
3132
"""生成Toast图片
3233
toast:str 背景图片(ADVANCEMENT,RECIPE,SYSTEM)
3334
image_path:str 图片路径(对应原版的物品位置)
3435
text1:str 第一行文本
3536
color1:str 第一行文本颜色
3637
text2:str 第二行文本
3738
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时作为保存路径,未指定就报错
3941
"""
4042
# 打开背景图片并缩放
4143
background_image = Image.open(toast)
@@ -67,14 +69,23 @@ def generate_image(toast:str, image_path:str, text1:str, color1:str, text2:str,
6769
draw.text((120, 26), text1, fill=color1, font=font)
6870
if text2 and color2:
6971
draw.text((120, 70), text2, fill=color2, font=font)
70-
72+
if resize:
73+
background_image = background_image.resize((320, 64),Image.Resampling.NEAREST)
7174
# 将 Pillow 图片转换为 PhotoImage
7275
if return_mode==RETURN_IMAGE:
73-
return background_image.resize((320, 64),Image.Resampling.NEAREST)
76+
return background_image
7477
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+
7687
else:
77-
return ImageTk.PhotoImage(background_image.resize((320, 64),Image.Resampling.NEAREST))
88+
return ImageTk.PhotoImage(background_image)
7889

7990
class ToastWindowUI:
8091
"""Toast界面类"""
@@ -108,7 +119,7 @@ def new_toast(self, toast=ADVANCEMENT, image_path=None, text1="一个弹窗", co
108119
for i in range(5):
109120
if toasts[i] == None:
110121
# 使用 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)
112123
self.root.deiconify()
113124
toasts[i] = self.canvas.create_image(320, i*64, anchor="nw", image=photo)
114125
event.set()
@@ -163,17 +174,20 @@ def set_no_focus(self):
163174
self.root.wm_attributes('-type', 'splash')
164175

165176
window=None
166-
def _init():
177+
def _init(e:Event=None):
167178
"""别调用"""
168179
global window
169180
window=ToastWindowUI()
181+
if e:
182+
e.set()
170183
window.main()
171184

172185
def init():
173186
"""初始化窗口"""
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()
177191

178192
def new_toast(toast=ADVANCEMENT, image_path:str=None, text1="一个弹窗", color1="yellow", text2="MCToast示例", color2="white"):
179193
"""新弹窗

mctoast/__main__.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import mctoast,sys
2+
help="""
3+
生成一个MCToast图片或者弹出一个Toast
4+
用法:
5+
mctoast -m <参数> [保存路径]
6+
7+
参数:
8+
--toast=<弹窗类型>,-t=<弹窗类型> 指定弹窗类型<0,1,2>
9+
0: 进度弹窗 (默认)
10+
1: 解锁配方弹窗
11+
2: 系统消息弹窗
12+
--image=<图片路径>,-i=<图片路径> 指定图片路径,默认"",即不显示图片
13+
--title=<标题>,-t1=<标题> 指定标题,默认"进度已达成!"
14+
--title-color=<颜色>,-c1=<颜色> 指定标题颜色,默认"yellow",你也可
15+
以使用"#RRGGBB"颜色代码
16+
--text=<文本>,-t2=<文本> 指定文本,默认"MCToast示例"
17+
--text-color=<颜色>,-c2=<颜色> 指定文本颜色,默认"white",你也可
18+
以使用"#RRGGBB"颜色代码
19+
--help,-h,-? 显示帮助
20+
[保存路径] 指定保存路径,默认"",即直接弹窗
21+
22+
显示位置:
23+
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
24+
┃ [--image] [--title,--title-color] ┃
25+
┃ [ ] [--text, --text-color] ┃
26+
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
27+
十分的直观 (
28+
29+
示例:
30+
python -m mctoast -t=0 -i=1.png -t1=进度已达成! -c1=yellow -t2=MCToast -c2=white
31+
32+
python -m mctoast -t=2 -t1=提示 "-t2=想不到吧 我有空格"
33+
34+
python -m mctoast -t=0 -i=1.png -t1=进度已达成! -c1=yellow "-t2=我好像被保存了. . ." -c2=white test.png
35+
36+
本mctoast具有超级牛力
37+
"""
38+
moo="""
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+
⠸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⣿⣿⣿⣿⣿⣿⣿⣿⣿⠂⠀⠀⠀
65+
⠀⠘⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡟⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀
66+
⠀⠀⠈⢿⣿⣿⣿⣿⣿⣿⣿⣿⠿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⠛⠛⠁⡇⠟⢿⣿⣿⣿⣿⣿⣿⣿⣧⡀⠀⠀
67+
68+
Rickrolled LOL
69+
"""
70+
print("MCToast 生成器 1.10\n")
71+
toasts=(mctoast.ADVANCEMENT,mctoast.RECIPE,mctoast.SYSTEM)
72+
toast=mctoast.ADVANCEMENT
73+
image=None
74+
text1="进度已达成!"
75+
color1="yellow"
76+
text2="MCToast示例"
77+
color2="white"
78+
savepath=None
79+
if len(sys.argv)>1:
80+
argv=sys.argv[1:]
81+
for arg in argv:
82+
if arg.startswith("--toast=") or arg.startswith("-t="):
83+
toast=arg.split("=")[1]
84+
elif arg.startswith("--image=") or arg.startswith("-i="):
85+
image=arg.split("=")[1]
86+
elif arg.startswith("--title=") or arg.startswith("-t1="):
87+
text1=arg.split("=")[1]
88+
elif arg.startswith("--title-color=") or arg.startswith("-c1="):
89+
color1=arg.split("=")[1]
90+
elif arg.startswith("--text=") or arg.startswith("-t2="):
91+
text2=arg.split("=")[1]
92+
elif arg.startswith("--text-color=") or arg.startswith("-c2="):
93+
color2=arg.split("=")[1]
94+
elif arg.startswith("--help") or arg.startswith("-h") or arg.startswith("-?"):
95+
print(help)
96+
exit(0)
97+
elif arg=="--moo":
98+
print(moo)
99+
exit(0)
100+
else:
101+
savepath=arg
102+
else:
103+
print("WARNING: 未指定参数,将弹出默认Toast,请使用 --help 查看帮助")
104+
if savepath==None:
105+
mctoast.init()
106+
mctoast.new_toast()
107+
else:
108+
mctoast.generate_image(toast,image,text1,color1,text2,color2,mctoast.RETURN_SAVETOFILE, False, savepath)
109+
print("已保存:",savepath)

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
setuptools.setup(
99
name="mctoast",
10-
version="1.01",
10+
version="1.10",
1111
description="把Minecraft的Toast带到现实里!",
1212
long_description=long_description,
1313
long_description_content_type="text/markdown",
@@ -21,7 +21,7 @@
2121
"License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)",
2222
"Operating System :: OS Independent",
2323
],
24-
requires=["pillow"],
24+
install_requires=["pillow"],
2525
license="GNU Lesser General Public License v3 (LGPLv3)",
2626
fullname="Minecraft Toast (tkinter)",
2727
url="https://github.com/SystemFileB/mctoast",

0 commit comments

Comments
 (0)