0

In Python turtle, I'm trying to print a circle when I click the screen, but it prints before I even click because the x,y coordinates are defined as variables.

I was trying to make tic tac toe and so far it makes the lines and when I click in a space I get my x, y coordinates. If they are within a range, they print a circle in that particular coordinate. It works well execpt for the middle square which prints without me clicking, right after I run the program.

The program I'm using to get coordinates is this:

xclick = 0
yclick = 0

def getcoordinates():
    screen.onscreenclick(modifyglobalvariables)


def modifyglobalvariables(rawx, rawy):
    global xclick
    global yclick
    xclick = int(rawx//1)
    yclick = int(rawy//1)

getcoordinates()

Here the starting value of xclick and yclick is 0, 0 so that's the problem. Is there a way to get around this? Where I don't need to initialise x, y as 0?

cdlane
  • 40,441
  • 5
  • 32
  • 81
  • 1
    Initialise them as `None` perhaps? Also I could be wrong but global variables seems a horrible way of handling that. – Peter Dec 23 '21 at 10:15
  • cant do none since i need integers – sourav mohile Dec 23 '21 at 10:23
  • 1
    You do a check if they're none and don't print – Peter Dec 23 '21 at 10:58
  • 1
    You seem to be fighting, possibly misunderstanding, how Python turtle screen click events work and this can lead to all sorts of problems. The click event should drive the action, not set globals that some other part of the code is waiting on to change. – cdlane Dec 23 '21 at 17:58

0 Answers0