Python Forum
Using Tkinter inside function not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using Tkinter inside function not working
#2
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)
Ensaimadeta likes this post
Reply


Messages In This Thread
RE: Using Tkinter inside function not working - by iMuny - Dec-22-2020, 01:46 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter destroy label inside labelFrame Nick_tkinter 3 4,711 Sep-17-2023, 03:38 PM
Last Post: munirashraf9821
  [Tkinter] Help running a loop inside a tkinter frame Konstantin23 3 1,744 Aug-10-2023, 11:41 AM
Last Post: Konstantin23
  Tkinter won't run my simple function AthertonH 6 4,127 May-03-2022, 02:33 PM
Last Post: deanhystad
  tkinter toggle buttons not working Nu2Python 26 7,420 Jan-23-2022, 06:49 PM
Last Post: Nu2Python
  [Tkinter] tkinter best way to pass parameters to a function Pedroski55 3 5,056 Nov-17-2021, 03:21 AM
Last Post: deanhystad
  Creating a function interrupt button tkinter AnotherSam 2 5,709 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 5,171 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  [Tkinter] Redirecting all print statements from all functions inside a class to Tkinter Anan 1 2,746 Apr-24-2021, 08:57 AM
Last Post: ndc85430
  tkinter get function finndude 2 3,057 Mar-02-2021, 03:53 PM
Last Post: finndude
  tkinter -- after() method and return from function -- (python 3) Nick_tkinter 12 7,745 Feb-20-2021, 10:26 PM
Last Post: Nick_tkinter

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020