I have problem with performed command by pressing button (using Tkinter).
I defined function like that:
refPt = []
cropping = False
def main(image_dir):
def click_and_crop(event, x, y, flags, param):
global refPt, cropping
if event == cv2.EVENT_LBUTTONDOWN:
refPt = [(x, y)]
cropping = True
elif event == cv2.EVENT_LBUTTONUP:
refPt.append((x, y))
cropping = False
cv2.rectangle(image, refPt[0], refPt[1], (0, 255, 0), 2)
cv2.imshow("image", image)
image=cv2.imread(image_dir)
print (image.dtype)
clone = image.copy()
cv2.namedWindow("image")
cv2.setMouseCallback("image", click_and_crop)
while True:
cv2.imshow("image", image)
key = cv2.waitKey(1) & 0xFF
if key == ord("r"):
image = clone.copy()
elif key == ord("c"):
break
if len(refPt) == 2:
roi = clone[refPt[0][1]:refPt[1][1], refPt[0][0]:refPt[1][0]]
cv2.imshow("ROI", roi)
cv2.waitKey(0)
cv2.imwrite('template.tif',roi)
cv2.destroyAllWindows()
It works fine, when i tested it alone.
This is small part of my GUI code:
window = tkinter.Tk() window.title("Analog2Digital Transform")
b2 = tkinter.Button(window, text="Start", command=main('7_7026_polowa.tif'), width=10, heigh=10)
Now, when i run my script function main()
is performed before showing GUI window. Additionally, it used argument of function - main('7_7026_polowa.tif')
, which is included in GUI code. Is it problem with function definition or GUI code?