我尝试构建这个多合一的图像转换器(基本上它只是 png、jpg、jpeg、webp),它应该让用户选择一个图像并将其转换为上述给定格式之一(一次一个) . 但是,当我尝试运行它时,只显示 getImage() & last func 按钮,中间的那个被跳过了。 输出
import tkinter as tk
from tkinter import filedialog
from PIL import Image
root = tk.Tk()
canvas1 = tk.Canvas(root, width = 300, height = 500, bg = 'azure3', relief = 'raised')
canvas1.pack()
label1 = tk.Label(root, text = 'File Conversion Tool', bg = 'azure3')
label1.config(font = ('helvetica', 20))
canvas1.create_window(150, 60, window = label1)
def getImage():
global im1
import_file_path = filedialog.askopenfilename()
im1 = Image.open(import_file_path)
browseButton_Image = tk.Button(text = " Import Image/File ", command = getImage, bg = 'royalblue', fg = 'white',
font = ('helvetica', 12, 'bold'))
canvas1.create_window(150, 130, window = browseButton_Image)
def convertToPNG():
global im1
export_file_path = filedialog.asksaveasfilename(defaultextension = '.png')
im1.save(export_file_path)
saveAsButton_PNG = tk.Button(text = 'Convert to PNG', command = convertToPNG, bg = 'royalblue', fg = 'white',
font = ('helvetica', 12, 'bold'))
canvas1.create_window(150, 180, window = saveAsButton_PNG)
def convertToJPG():
global im1
export_file_path = filedialog.asksaveasfilename(defaultextension = '.jpg')
im1.save(export_file_path)
saveAsButton_JPG = tk.Button(text = 'Convert to JPG', command = convertToJPG, bg = 'royalblue', fg = 'white',
font = ('helvetica', 12, 'bold'))
canvas1.create_window(150, 180, window = saveAsButton_JPG)
def convertToJPEG():
global im1
export_file_path = filedialog.asksaveasfilename(defaultextension = '.jpeg')
im1.save(export_file_path)
saveAsButton_JPEG = tk.Button(text = 'Convert to JPEG', command = convertToJPEG, bg = 'royalblue', fg = 'white',
font = ('helvetica', 12, 'bold'))
canvas1.create_window(150, 180, window = saveAsButton_JPEG)
def convertToWebP():
global im1
export_file_path = filedialog.asksaveasfilename(defaultextension = '.webp')
im1.save(export_file_path)
saveAsButton_WebP = tk.Button(text = 'Convert to WebP', command = convertToWebP, bg = 'royalblue', fg = 'white',
font = ('helvetica', 12, 'bold'))
canvas1.create_window(150, 180, window = saveAsButton_WebP)
root.mainloop()