import tkinter as tk
from tkinter import filedialog
from PIL import Image
def open_img():
try:
global img
filepath = filedialog.askopenfilename()
filename.set(filepath)
img = Image.open(filename.get())
except Exception as e:
print("您没有选择任何文件", e)
def save_png():
try:
filetypes = [("PNG", "*.png"), ("JPG", "*.jpg"), ("GIF", "*.gif"), ("txt files", "*.txt"), ('All files', '*')]
filenewpath = filedialog.asksaveasfilename(title='保存文件',
filetypes=filetypes,
defaultextension='.png',
initialdir='C:/Users/Administrator/Desktop')
path_var.set(filenewpath)
img.save(str(path_var.get()))
except Exception as e:
print(e)
window = tk.Tk()
window.title("逻辑网")
window.geometry('400x200+300+300')
window.iconbitmap('../image/icon.ico')
filename = tk.StringVar()
path_var = tk.StringVar()
entry = tk.Entry(window, textvariable=filename)
entry.grid(row=1, column=0, padx=5, pady=5)
tk.Button(window, text='选择文件', command=open_img).grid(row=1, column=1, padx=5, pady=5)
entry1 = tk.Entry(window, textvariable=path_var)
entry1.grid(row=2, column=0, padx=5, pady=5)
tk.Button(window, text='保存文件', command=save_png).grid(row=2, column=1, padx=5, pady=5)
window.mainloop()