antcolony
antcolony

Reputation: 83

Scheduling order of events with tkinter OOP

I am using tkinter in python 3.6 to display a blue square in the middle of a window. At each click the blue square should disappear and reappear after 2 seconds on a different random location. When running the following code, the blue square (referred as stimulus) does not disappear. Everything else seem to work properly.

This is the code:

import tkinter as TK
import random as RAN

class THR:
    def __init__(self, root):
        self.root = root
        self.root.config(background='black')

        self.screenYpixels = 600
        self.screenXpixels = 1024
        self.ITI = 2000

        self.background = TK.Canvas(root, width=1024, height=600, bg='black',
                                    bd=0, highlightthickness=0, relief='ridge')

        self.background.pack()
        self.newtrial()

    def newtrial(self):
        self.xpos = RAN.randrange(200, 1000)
        self.ypos = RAN.randrange(100, 500)

        self.stimulus = TK.Canvas(root,width=100,height=100,bg='blue', bd=0, 
                                  highlightthickness=0, relief='ridge')
        self.stimulus.place(x=self.xpos, y=self.ypos, anchor="c")
        self.stimulus.bind("<Button-1>", self.response)

        self.exitbutton()

    def response(self, event):
        self.stimulus.place_forget()
        self.intertrialinterval()

    def intertrialinterval(self, *args):
        self.root.after(self.ITI,self.newtrial())

    def exitbutton(self):
        self.exitButton = TK.Button(self.root, bg="green")
        self.exitButton.place(relx=0.99, rely=0.01, anchor="c")
        self.exitButton.bind("<Button-1>", self.exitprogram)

    def exitprogram(self, root):
        self.root.quit()


root = TK.Tk()
THR(root)
root.mainloop()

Here a list of things I tried but that did not work

The last couple of hours searching the web for similar problems / solutions did not really help. I would like to fix this and at the same time understand what am I doing wrong.

Upvotes: 2

Views: 61

Answers (1)

antcolony
antcolony

Reputation: 83

As it turns out the list of links on the right of this webpage provided me with the solution just 3 minutes after posting the question.

This is the original thread Python Tkinter after event OOPS implementation

and here I write the solution: dropping the parenthesis in the function called with the after method. So this self.root.after(self.ITI,self.newtrial()) is self.root.after(self.ITI,self.newtrial)

Unfortunately I still do not understand how that fixed the problem..

Upvotes: 2

Related Questions