Dec-22-2020, 01:46 PM
the canvas object you created is not visible to the paintRectangle[1-4] methods due to being out of scope of the subject methods, you can either adopt an object oriented approach to the above problem or you can pass an instance of the canvas object when you call the paintRectangle[1-4] methods,
def paintRectangle1(canvas_ref): canvas_ref.create_rectangle(200, 120, 400, 220, fill='red') time.sleep(0.3) def paintRectangle2(canvas_ref): canvas_ref.create_rectangle(600, 120, 800, 220, fill='red') time.sleep(0.3) def paintRectangle3(canvas_ref): canvas_ref.create_rectangle(200, 260, 400, 360, fill='red') time.sleep(0.3) def paintRectangle4(canvas_ref): canvas_ref.create_rectangle(600, 260, 800, 360, fill='red') time.sleep(0.3)and you can call the methods like
canvas = Canvas( root, width = 1280, height = 800) if (19 < x < 60) and (20 < y < 40): print("OK button1") paintRectangle1(canvas) if (68 < x < 110) and (20< y < 40): print("OK button2") paintRectangle2(canvas) if (19 < x < 60) and (45 < y < 80): print("OK button3") paintRectangle3(canvas) if (68 < x <110) and (45 < y < 80): print("OK button4") paintRectangle4(canvas)