在 tkinter(图像转换器)中添加多个按钮

add multiple buttons in tkinter(image converter)

我尝试构建这个一体式图像转换器(基本上它只是 png、jpg、jpeg、webp),它应该让用户 select 一张图像并将其转换为以上给定的格式(一次一个)。但是,当我尝试 运行 时,它只显示 getImage() 和最后一个 func 按钮,中间的按钮被跳过。 output

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()

从代码看,最后4个按钮的x,y位置相同:150, 180。结果,4 个按钮重叠了。从前 2 个按钮看,您的按钮的 y 位置增加了 50。因此将 50 添加到每个之前按钮的 y 位置。

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)

...

saveAsButton_JPG = tk.Button(text = 'Convert to JPG', command = convertToJPG, bg = 'royalblue', fg = 'white', font = ('helvetica', 12, 'bold'))
canvas1.create_window(150, 230, window = saveAsButton_JPG)

...

saveAsButton_JPEG = tk.Button(text = 'Convert to JPEG', command = convertToJPEG, bg = 'royalblue', fg = 'white', font = ('helvetica', 12, 'bold'))
canvas1.create_window(150, 280, window = saveAsButton_JPEG)

...

saveAsButton_WebP = tk.Button(text = 'Convert to WebP', command = convertToWebP, bg = 'royalblue', fg = 'white',font = ('helvetica', 12, 'bold'))
canvas1.create_window(150, 330, window = saveAsButton_WebP)

...

root.mainloop()