The image displayed in the GUI moves down every time user selects a newone (Tkinter, Python)

Viewed 26

I'm using tkinter to make a simple GUI and implement a neural network that I trained. The problem is, every time the user uses the button to select a new image to make a prediction, the entire gui moves down. Here pictures of what's happening

First image selected and predicted

Second image selected and predicted

Here's my code:

class App:
    
    def __init__(self):

        self.root = tk.Tk()
        self.root.title("Detector de Melanomas")
        self.image = ""
        
        global model
        model = load_model(os.path.join('models', 'detectormelanomas-NuevasImagenesMelanoma2.h5'))
        window_width = 700
        window_height = 700
        # get the screen dimension
        screen_width = self.root.winfo_screenwidth()
        screen_height = self.root.winfo_screenheight()

        # find the center point
        center_x = int(screen_width/2 - window_width / 2)
        center_y = int(screen_height/2 - window_height / 2)

        # set the position of the window to the center of the screen
        self.root.geometry(f'{window_width}x{window_height}+{center_x}+{center_y}')
        
        
        titulo = ttk.Label(self.root, text="Detector de Melanomas", font=("Arial Bold", 12))
        titulo.pack(anchor=CENTER)
        
        file_upload = ttk.Button(self.root, text="seleccione la imagen sospechosa",
                                 command=lambda: self.upload_file())
        file_upload.pack()

        self.root.resizable(False, False)
        
        

        # when loading a file in, root.lower()

    def run(self):
        self.root.mainloop()

    def upload_file(self):
        file_path = fd.askopenfile(mode='r', filetypes=[('jpg files', '*.jpg'),
        ('jpeg files', '*.jpeg'),
        ('jpg files', '*.png'),
        ('All files', '*.*')])
        if file_path is not None:
            pass
        
        
        img = ImageTk.PhotoImage(Image.open(file_path.name))
        self.image = img

        output = tk.Label(self.root, text=file_path.name, image=self.image)
        output.pack()
        

        imagen = cv2.imread(file_path.name)
        imagen = tf.image.resize(imagen, (224,224), preserve_aspect_ratio=True)
        pred = model.predict(np.expand_dims(imagen/255, 0))
        pred_label = ""
        if pred > 0.5:
            print(f'La prediccion es Melanoma')
            pred_label = "La prediccion es Melanoma"
        else:
            print(f'La prediccion es Lunar')
            pred_label = "La prediccion es Lunar"
        prediccion = ttk.Label(self.root, text=pred_label, font=("Arial Bold", 12))
        prediccion.pack(anchor=CENTER)
new_app = App()
new_app.run()
0 Answers
Related