1
import tkinter as tk
from tkinter import filedialog
from PIL import ImageTk, Image
import os
import argparse
import numpy as np

db_path = "/fulldatabase/"
output = None
output_path = None


def test():
    root = tk.Tk()
    root.withdraw()
    dirname = tk.filedialog.askdirectory(parent=root, initialdir="./", title='select input query folder')
    root.destroy()
    return (dirname)


if __name__ == "__main__":
    in_dir = test()
    path = os.listdir(in_dir)
    parser = argparse.ArgumentParser()
    parser.add_argument('--output', '-o')
    args = parser.parse_args()
    output_path = args.output
    window = tk.Tk()
    window.title("My App")
    window.geometry("1024x800")
    app = Application(window, in_dir, path)
    window.mainloop()
    output.close()

I have an input query folder with a set of different flower images say rose_3, lily_5, and jasmine_2 where the digit after underscore represents its instance number.

I want to select an image from the input folder and display it on a tkinter window.

I also want to display all other images of roses (which is read from the full database, i.e another folder) say rose_1, rose_2, rose_4, and rose_5 (if I have 5 images for roses in the database) on the same tkinter window on its right side one below the other at the same time.

All this is working perfectly fine with this code, but the GUI often freezes. Can anybody help me with this?

Sara S
  • 153
  • 5
  • You mean is that GUI will freeze when you load image ,right? – jizhihaoSAMA Feb 06 '20 at 04:48
  • @jizhihaoSAMA yes, correct – Sara S Feb 06 '20 at 04:53
  • The freeze may be caused by the for loop inside `add_right_panel()` function, try adding `self.right_panel.update_idletasks()` after `self.right_panel.pack(...)`. Also, it is not good to use `Label` as container, like `self.panel1`, `self.panel2` and `self.panel3`. Use `Frame` instead. – acw1668 Feb 06 '20 at 08:16

2 Answers2

1

Maybe you should use do it with a thread. Create a thread to load your Image and examine whether the Image finished with loading. And return a value whether it is finished.

jizhihaoSAMA
  • 12,336
  • 9
  • 27
  • 49
  • PS: I make a GUI function to examine whether this app's version is the latest. So I create a thread to crawl my github release page.And when it is crawling,My GUI will show a tip "Wait for a moment".And my GUI won't freeze. – jizhihaoSAMA Feb 06 '20 at 05:02
  • Could you please add that update to my code? I'm a beginner in python and not familiar with threads. – Sara S Feb 06 '20 at 05:04
  • But I think you should learn about threading module,Read more about https://docs.python.org/3.7/library/threading.html – jizhihaoSAMA Feb 06 '20 at 05:13
0

You should do the loading (and possible further processing) of the image in a separate thread.

Create a thread where youn load your Image and once that task I done you can send the image for rendering to the GUI.

Most if not even all GUI frameworks use a main thread (with a loop constantly running inside) for specifically handling GUI-related tasks such as processing user input (mouse, keyboard etc.), rendering and so on. Once you introduce a different load in that thread (like loading an image) the thread is no longer capable of concentrating only on the GUI-specific tasks. This leads to a freezing in your GUI.

rbaleksandar
  • 8,713
  • 7
  • 76
  • 161
  • Could you please add that update to my code? I'm a beginner in python and not familiar with threads. – Sara S Feb 06 '20 at 05:10
  • @SaraS Multithreading is not a Python-specific thing. I would suggest doing some research of general multithreaded programming. You will have the same issues with Qt, wxWidgets, Windows Forms, Swing etc. On Stackoverflow there are many questions just like yours (perhaps not loading images of flowers :-3). [Example](https://stackoverflow.com/a/16747734/1559401) – rbaleksandar Feb 06 '20 at 06:23