Título: Opiniones sobre un programa que hice
Publicado por: Panic0 en 5 Septiembre 2021, 05:43 am
Hola a todos como están? Hace poco hice un programa en Python usando Tkinter, este es un programa que sirve para hacer un png to jpg (Un convertidor de formatos). Es algo muy simple, y me gustaría que le den una critica al código. import tkinter from tkinter import Button, Canvas, Frame, Label, LabelFrame, PhotoImage, StringVar, Text, Tk from tkinter.constants import ACTIVE, CENTER, DISABLED, FLAT, NONE, NW, RADIOBUTTON import tkinter.font as font from tkinter import filedialog as fd from tkinter import messagebox
import shutil import os
from PIL import Image,ImageTk
def exit() -> None: global root root.destroy()
def converter() -> None: global image_path global image_name
try: try: im = Image.open(image_path)
except NameError: messagebox.showerror(title="Error!", message="You must select a file!")
#I convert the image to jpg im.save(f'output_file_jpg/{image_name}', quality=95) img_name_var.set(image_name)
except OSError: #If the image has transparency I remove it rgb_im = Image.open(image_path) rgb_im = im.convert('RGB') rgb_im.save(f'output_file_jpg/{image_name}') img_name_var.set(image_name) image_path = "" image_name = "" messagebox.showinfo(title="Success!",message="successfully converted!")
def get_image() -> None: global image_path global image_name
try: #I look for the route and the name of the image image_path = fd.askopenfilenames(filetypes=[("Image Files", ".png")]) image_path = image_path[0] image_name = image_path.split("/") image_name = image_name[-1] img_name_var.set(image_name) image_name = image_name.replace('.png',".jpg")
#Display Image img = Image.open(image_path) img = img.resize((180, 160), Image.ANTIALIAS) img = ImageTk.PhotoImage(img) panel = Label(master=root, image=img) panel.image = img panel.grid(row=1,column=0,pady=1,sticky="N") bttn_convert['state'] = ACTIVE
except IndexError: pass
def main_window() -> None: try: os.mkdir('output_file_jpg') except FileExistsError: pass global root global image_path global img_name_var global bttn_convert
#Main Window config root.config(background="white") root.resizable(False,False) root.title("V1.0")
screen_width = root.winfo_screenwidth() screen_height = root.winfo_screenheight() screen_width = screen_width / 5 root.geometry(f"600x450+{round(screen_width)}+90")
#Cool Colors navy_blue = "#2A73D9" emerald_green = "#33b864" soft_red = "#b83333"
#Cool Fonts bttn_font = font.Font(family="Helvetica,Arial,sans-serif", size=12) title_font = font.Font(family="Poppins",size=15) subtitle_font = font.Font(family="Poppins",size=12)
#Texts title = Label(master=root) title.config(text="CONVERTER",bg="white",font=subtitle_font) title.grid(row=0,column=0,pady=30)
subtitle = Label(master=root) subtitle.config(text="PNG TO JPG",bg="white",font=title_font) subtitle.grid(row=0,column=0,sticky="N")
softwere_version = Label(master=root) softwere_version.config(text="V1.0",background="white") softwere_version.grid(row=1,column=0,padx=20,pady=120,sticky="SW")
img_name_var = StringVar(master=root,value='')
img_name = Label(master=root) img_name.configure(textvariable=img_name_var,bg="white") img_name.grid(row=1,column=0,pady=180,sticky="N")
#Buttons frame_buttons = Frame(master=root) frame_buttons.config(bg="white") frame_buttons.grid(row=1,column=0,pady=220,padx=110)
bttn_upload = Button(master=frame_buttons) bttn_upload.configure(text="UPLOAD A IMAGE",font=bttn_font,activeforeground="white",fg="white",activebackground=navy_blue,bg=navy_blue,borderwidth=0,cursor="hand2",command=get_image) bttn_upload.grid(row=1,column=1,padx=20)
bttn_convert = Button(master=frame_buttons) bttn_convert['state'] = DISABLED bttn_convert.configure(text="CONVERT",font=bttn_font,activeforeground="white",fg="white",activebackground=emerald_green,bg=emerald_green,borderwidth=0,cursor="hand2",command=converter) bttn_convert.grid(row=1,column=2)
bttn_exit = Button(master=frame_buttons) bttn_exit.configure(text="EXIT",font=bttn_font,activeforeground="white",fg="white",activebackground=soft_red,bg=soft_red,borderwidth=0,cursor="hand2",command=exit) bttn_exit.grid(row=1,column=3,padx=15)
if __name__ == "__main__": root = Tk()
main_window() root.mainloop()
Gracias!!!! ::) ::) :D -Panic0
Título: Re: Opiniones sobre un programa que hice
Publicado por: Drakaris en 5 Septiembre 2021, 11:41 am
Buenas!! Una buena practica para facilitar al usuario la ejecución del programa es indicar en un archivo requeriments.txt todos los modulos neccesarios para el script, eso se hace con pip freeze > requeriments.txt , he incluso aconsejaria los comandos de instalacion de cada modulo,opcionalmente.
Título: Re: Opiniones sobre un programa que hice
Publicado por: Drakaris en 5 Septiembre 2021, 11:49 am
En la primera ejecución me dio un error. Traceback (most recent call last): File "/usr/lib/python3.9/tkinter/__init__.py", line 1892, in __call__ return self.func(*args) File "/home/nitropc/./png.py", line 26, in converter im = Image.open(image_path) File "/usr/local/lib/python3.9/dist-packages/PIL/Image.py", line 2974, in open fp = io.BytesIO(fp.read()) AttributeError: 'str' object has no attribute 'read' ^CTraceback (most recent call last): File "/home/nitropc/./png.py", line 155, in <module> root.mainloop() File "/usr/lib/python3.9/tkinter/__init__.py", line 1429, in mainloop self.tk.mainloop(n)
El error se debe a que en el metodo , el argumento sebe ser un string y la variable image_path parece no ser string, así que aconsejo que siempre se convierta en string, auque cuya variable sea string (por si acaso), hazlo con str(image_path), seria Image.open(str(image_path))
Y aconsejo también que muestra la ruta del archivo png convertido que no especifica en que ruta se almacena.
Título: Re: Opiniones sobre un programa que hice
Publicado por: Drakaris en 5 Septiembre 2021, 11:50 am
Por lo otro muy bien, enhorabuena
Título: Re: Opiniones sobre un programa que hice
Publicado por: Drakaris en 5 Septiembre 2021, 11:51 am
Puedes convertirlo a .exe para windows, eso lo puedes hacer con pyinstaller
Título: Re: Opiniones sobre un programa que hice
Publicado por: Panic0 en 5 Septiembre 2021, 14:44 pm
En la primera ejecución me dio un error. Traceback (most recent call last): File "/usr/lib/python3.9/tkinter/__init__.py", line 1892, in __call__ return self.func(*args) File "/home/nitropc/./png.py", line 26, in converter im = Image.open(image_path) File "/usr/local/lib/python3.9/dist-packages/PIL/Image.py", line 2974, in open fp = io.BytesIO(fp.read()) AttributeError: 'str' object has no attribute 'read' ^CTraceback (most recent call last): File "/home/nitropc/./png.py", line 155, in <module> root.mainloop() File "/usr/lib/python3.9/tkinter/__init__.py", line 1429, in mainloop self.tk.mainloop(n)
El error se debe a que en el metodo , el argumento sebe ser un string y la variable image_path parece no ser string, así que aconsejo que siempre se convierta en string, auque cuya variable sea string (por si acaso), hazlo con str(image_path), seria Image.open(str(image_path))
Y aconsejo también que muestra la ruta del archivo png convertido que no especifica en que ruta se almacena.
Hola cómo estás? Probando y probando el programa no me sale ese error, es raro. Lo probé desde Windows y si paso jajaja.
Título: Re: Opiniones sobre un programa que hice
Publicado por: Panic0 en 5 Septiembre 2021, 14:45 pm
Por lo otro muy bien, enhorabuena
Muchísimas gracias por probarlo y responder!!!!
|