天天看點

打開資料總管 打開圖檔 儲存圖檔 python學習

import tkinter as tk
from tkinter import filedialog, dialog
import os
from PIL import Image
window = tk.Tk()
window.title('視窗') 
window.geometry('500x500')
 
file_path = ''
 
file_text = ''
save_list=[]
def open_file():
    global file_path
    global file_text
    file_path = filedialog.askopenfilename()
    image = Image.open(file_path)
    image.show()
    save_list.append(image)

def save_file(x):
    print(x)
    global file_path
    x.show
    file_path = filedialog.asksaveasfilename()
    x.save(file_path)
    print('儲存檔案', file_path)

bt1 = tk.Button(window, text='打開檔案', width=15, height=2, command=open_file)
bt1.pack()
bt2 = tk.Button(window, text='儲存檔案', width=15, height=2, command=lambda :save_file(save_list[0]))
bt2.pack() 
window.mainloop()