0

我尝试构建这个多合一的图像转换器(基本上它只是 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()
4

1 回答 1

1

从代码看,最后 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()
于 2021-08-05T12:46:24.333 回答