I'm new to python GUI programming, I have a button that when you click on it, takes time to run because of calculation time. all I want is to show a 'waiting' word in the middle of a label when my main function is running.
Here is my code where Macula(file_path):
function takes time to run and I try to put a Label
before calling M.makula_detection(image, thresh, n, Size)
but as I know, we can't show label features until the function ends.
I also tried to use two functions in on command but it doesn't answer too because they run together.
Here is my code:
from tkinter import *
from PIL import ImageTk, Image
from tkinter import filedialog
import cv2 as cv
import MaskGeneration as mg
import MakulaDetection as M
# =================================== statics and configuration ===================================
color = '#20536C'
root = Tk()
root.title('Opticdisk and Macula detector')
root.configure(bg= color)
root.geometry('1070x700')
root.resizable(width=False, height=False)
root.iconbitmap('J:\Projects\Bachelor Project\download.ico')
# =================================== functions and body ===================================
filename_path = {}
# label = Label(LEFT,text = 'fff')
def open_image(file_path):
global select_lbl
bigIm_path = {}# the new path of resized big image
file_path['image'] = filedialog.askopenfilename(initialdir="J://uni//final project//Data set",
title="select an image",
filetypes=(('all files', '*.*'), ('jpg files', '*.jpg'), ('tif file','*.tif')))
IM = cv.imread(file_path['image'])
im_wid = len(IM)
im_len =len(IM[1])
if (im_len > 749 and im_wid > 630) or (im_len>749) or (im_wid>630):
IM = cv.resize(IM, (629, 629))
cv.imwrite('J://uni//final project//res_image//big_img.jpg', IM)
bigIm_path['image'] = ('J://uni//final project//res_image//big_img.jpg')
#top line save the new resized big image in bigIM_path{}
mainImage = ImageTk.PhotoImage(Image.open(bigIm_path['image']))
select_lbl = Label(left, image=mainImage,
width=749,
height=630,
bg='#020101') # .place(x=20, y=0)
select_lbl.image = mainImage # keep a reference! to show the image
select_lbl.place(x=0, y=0)
else:
mainImage = ImageTk.PhotoImage(Image.open(filename_path['image']))
select_lbl = Label(left, image=mainImage,
width= 749,
height=630,
bg='#020101')#.place(x=20, y=0)
select_lbl.image = mainImage # keep a reference! to show the image
select_lbl.place(x=0, y=0)
def Macula(file_path):
path={}#
# messagebox = Message(left).place(x=20,y=10)
try:
# select_lbl.pack_forget()
image = cv.imread(file_path['image'])
image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
# top line solve the problem of blue background color by changing color space from BGR to RGB
Size = 0.2
thresh = 25
n = 80
M.makula_detection(image, thresh, n, Size)
path['image'] = ('J://uni//final project//res_image//marked_M.png')
''' this line is a tec for dispaly the file '''
mainImage = ImageTk.PhotoImage(Image.open(path['image']))
lbl = Label(left, image=mainImage,
width=749,
height=630,
bg='#020101') # .place(x=20, y=0)
lbl.image = mainImage # keep a reference! to show the image
lbl.place(x=0, y=0)
except:
print('error')
lbl = Label(left,text = "No image file selected !!!",
font = ('Times', 25, 'italic', 'bold'),
fg = '#ffe874',
bg = color)
lbl.place(x=200,y=270)
def Mask(file_path):
path = {}
# messagebox = Message(left).place(x=20,y=10)
try:
Im = cv.imread(file_path['image'])
mask = mg.mask_generation(Im)
# dispaly function
I = cv.resize(mask, (400, 400))
cv.imwrite('J://uni//final project//res_image//finalresult.jpg', I)
path['image'] = ('J://uni//final project//res_image//finalresult.jpg')
''' this line is a tec for dispaly the file '''
mainImage = ImageTk.PhotoImage(Image.open(path['image']))
lbl = Label(left, image=mainImage,
width=749,
height=630,
bg='#020101') # .place(x=20, y=0)
lbl.image = mainImage # keep a reference! to show the image
lbl.place(x=0, y=0)
except:
print('error')
lbl = Label(left, text="No image file selected !!!",
font=('Times', 25, 'italic', 'bold'),
fg='#03283a',
bg=color)
lbl.place(x=200, y=270)
# lbl.grid(row=5,column=10)
# =================================== Buttons ===================================
btnBrowse = Button(top, width=93,
text='select file',
fg='#58859a',
font=('Times', 15, 'italic', 'bold'),
bg='#03283a',
command = lambda :open_image(filename_path))
btnBrowse.pack(side=BOTTOM)
btnMask = Button(right, text='Image Mask',
fg= '#58859a',
font=('Times', 20, 'italic', 'bold'),
bg="#03283a",
width=19,
height=6,
command=lambda:Mask(filename_path) )
btnMask.pack(side=TOP)
btnMacula = Button(right, text='Macula',
fg= '#58859a',
font=('Times', 20, 'italic', 'bold'),
bg="#03283a",
width=19,
height=6,
command=lambda :Macula(filename_path))
btnMacula.pack(side=TOP)
btnClear = Button(right, text='exit',
fg= '#58859a',
font=('Times', 20, 'italic', 'bold'),
bg="#03283a",
width=19,
height=6,
command=root.quit)
btnClear.pack(side=TOP)
root.mainloop()
Is this even possible?