import tkinter as tk
from tkinter import filedialog
from PIL import Image
from tkinter import messagebox
from tkinter import ttk
import os
# 创建窗口
window = tk.Tk()
window.title("ico转换器")
window.geometry("250x150")
# 创建Notebook小部件
notebook = ttk.Notebook(window)
notebook.pack(fill=tk.BOTH, expand=True)
# 第一页 - 选择文件页面
select_file_frame = tk.Frame(notebook)
notebook.add(select_file_frame, text=" 选择目标文件 ")
target_label = tk.Label(select_file_frame, text="目标文件:")
target_label.pack()
target_entry = tk.Entry(select_file_frame)
target_entry.pack()
def select_file():
filepath = filedialog.askopenfilename(filetypes=[("文件:", "*.jpg;*.jpeg;*.png;")])
target_entry.delete(0, tk.END)
target_entry.insert(0, filepath)
file_btn = tk.Button(select_file_frame, text="选择文件", command=select_file)
file_btn.pack()
# 第二页 - 选择路径页面
select_path_frame = tk.Frame(notebook)
notebook.add(select_path_frame, text=" 选择保存路径 ")
save_label = tk.Label(select_path_frame, text="保存到:")
save_label.pack()
save_entry = tk.Entry(select_path_frame)
save_entry.pack()
def select_path():
path = filedialog.askdirectory()
save_entry.delete(0, tk.END)
save_entry.insert(0, path)
path_btn = tk.Button(select_path_frame, text="选择路径", command=select_path)
path_btn.pack()
# 定义图片转换为ICO函数
def convert_to_ico():
target_file = target_entry.get()
save_path = save_entry.get()
try:
img = Image.open(target_file)
filename = os.path.splitext(os.path.basename(target_file))[0]
img.save(save_path + "/" + filename + ".ico", format="ICO")
messagebox.showinfo("转换完成", "图片已成功转换为ICO文件!")
except Exception as e:
messagebox.showerror("转换失败,文件不正确或者路径不正确", str(e))
# 创建转换按钮
convert_btn = tk.Button(window, text="开始转换", command=convert_to_ico)
convert_btn.pack()
# 运行窗口
window.mainloop()
li642041156
- 粉丝: 5
- 资源: 31