Hello, so I've been working on a simple Tic Tac Toe game using TKinter graphics GUI. However i keep getting this error, and i'm not sure why. Any help would be greatly appreciated.

Heres the code

import graphics
import random

def constructBoard():
    win=graphics.GraphWin("Tic Tac Toe",500,500)
    win.setCoords(0.0,0.0,3.0,3.0)
    line1=graphics.Line(graphics.Point(1,0), graphics.Point(1,3)).draw(win)
    line2=graphics.Line(graphics.Point(2,0), graphics.Point(2,3)).draw(win)
    line3=graphics.Line(graphics.Point(0,1), graphics.Point(3,1)).draw(win)
    line4=graphics.Line(graphics.Point(0,2), graphics.Point(3,2)).draw(win)

def player1Move(win):
    mouseClick = win.getMouse()
    x=mouseClick.getX()
    y=mouesClick.getX()
    radius = random.randint(5, 20)
    topL = graphics.Point(x-radius, y-radius)
    botR = graphics.Point(x+radius, y+radius)
    player1X = graphics.Line(graphics.Point(topL, topL), graphics.Point(botR, botR))
    player1X.draw(win)
    
    
def player2Move(win):
    mouseClick = win.getMouse()
    x = mouseClick.getX()
    y = mouseClick.getX()
    radius = random.randint(5,20)
    topL = graphics.Point(x-radius, y-radius)
    botR = graphics.Point(x+radius, y+radius)
    player2Circle = graphics.Circle(topL, botR)
    player2Circle.draw(win)
    

def main():
    win = constructBoard()
    player1Win = False
    player2Win = False
    while player1Win == False and player2Win == False:
        player1Move(win)
        player2Move(win)

Heres the error

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    main()
  File "C:/Python25/TicTacToe.py", line 43, in main
    player1Move(win)
  File "C:/Python25/TicTacToe.py", line 17, in player1Move
    mouseClick = win.getMouse()
AttributeError: 'NoneType' object has no attribute 'getMouse'

again any help would be greatly appreciated, thanks!

Recommended Answers

All 5 Replies

]AttributeError: 'NoneType' object has no attribute 'getMouse'

"win" = constructBoard() = None as constructBoard doesn't return anything. Also, python naming conventions use lower case with underscores for function names, and there it is poor style to construct a separate "main" function, especially since it is never called.

okay, i changed it around a little bit but now i get another error.

heres the code modified a little.

import graphics
import random

def constructBoard():
    win=graphics.GraphWin("Tic Tac Toe",500,500)
    win.setCoords(0.0,0.0,3.0,3.0)
    line1=graphics.Line(graphics.Point(1,0), graphics.Point(1,3)).draw(win)
    line2=graphics.Line(graphics.Point(2,0), graphics.Point(2,3)).draw(win)
    line3=graphics.Line(graphics.Point(0,1), graphics.Point(3,1)).draw(win)
    line4=graphics.Line(graphics.Point(0,2), graphics.Point(3,2)).draw(win)
    return win

def player1Move(win):
    mouseClick = win.getMouse()
    x=mouseClick.getX()
    y=mouseClick.getX()
    radius = random.randint(5, 20)
    topL = graphics.Point(x-radius, y-radius)
    botR = graphics.Point(x+radius, y+radius)
    player1X = graphics.Line(graphics.Point(topL, topL), graphics.Point(botR, botR))
    player1X.draw(win)
    
    
def player2Move(win):
    mouseClick = win.getMouse()
    x = mouseClick.getX()
    y = mouseClick.getX()
    radius = random.randint(5,20)
    topL = graphics.Point(x-radius, y-radius)
    botR = graphics.Point(x+radius, y+radius)
    player2Circle = graphics.Circle(topL, botR)
    player2Circle.draw(win)
    
def main():
    win = constructBoard()
    player1Win = False
    player2Win = False
    while player1Win == False and player2Win == False:
        player1Move(win)
        player2Move(win)

and heres the error i get.

>>> main()
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__

Traceback (most recent call last):    return self.func(*args)

  File "C:\Python27\lib\lib-tk\Tkinter.py", line 495, in callit
    func(*args)
  File "<pyshell#7>", line 1, in <module>
  File "C:/Python25\graphics.py", line 177, in _tk_pump
    main()
    result = command()
  File "C:/Python25/TicTacToe.py", line 45, in main
  File "C:/Python25\graphics.py", line 196, in func
    player2Move(win)
    return f(*args, **kw)
  File "C:/Python25/TicTacToe.py", line 29, in player2Move
  File "C:/Python25\graphics.py", line 634, in _draw
    mouseClick = win.getMouse()
    x1,y1 = canvas.toScreen(p1.x,p1.y)
  File "C:/Python25\graphics.py", line 321, in getMouse
  File "C:/Python25\graphics.py", line 354, in toScreen
    _tkCall(self.update)
    return self.trans.screen(x,y)
  File "C:/Python25\graphics.py", line 194, in _tkCall
  File "C:/Python25\graphics.py", line 391, in screen
    raise GraphicsError, DEAD_THREAD
    xs = (x-self.xbase) / self.xscale
GraphicsError: Graphics thread quit unexpectedly
TypeError: unsupported operand type(s) for -: 'instance' and 'float'

again thanks for your help, I appreciate it a lot!

I do not know about the error you are now getting, but I see other problems. You define two variables x and y to same value which is confusing. And you are not changing variables any time inside your while loop which leads to program not stopping as you have also not return or break statements to come out of it.

I did the drawing routine, unfortunately the result had little to do with your start, so you have still long to go (construct_board is OK, at least, only named in camelCase instead of standard way).

The error you got is because you prepare Point object and then try to give that Point object as parameter while constructing other Point object at line 20.

Could you explain the purpose of lines 17 and 28 in your algorithm?

What happens when radius is greater than x or y. I get something like
x, y, radius 0.492985971944 0.492985971944 13 --> -12.5070140281 -12.5070140281
(And note that we will not do your homework for you, so if this gobbledygook was thrown together with the hope that we would code it for you, you are going to be disappointed)

radius = random.randint(5, 20)
topL = graphics.Point(x-radius, y-radius)
import graphics
import random

def constructBoard():
    win=graphics.GraphWin("Tic Tac Toe",500,500)
    win.setCoords(0.0,0.0,3.0,3.0)
    line1=graphics.Line(graphics.Point(1,0), graphics.Point(1,3)).draw(win)
    line2=graphics.Line(graphics.Point(2,0), graphics.Point(2,3)).draw(win)
    line3=graphics.Line(graphics.Point(0,1), graphics.Point(3,1)).draw(win)
    line4=graphics.Line(graphics.Point(0,2), graphics.Point(3,2)).draw(win)
    return win

def player_common(win):
    mouseClick = win.getMouse()
    x=mouseClick.getX()
    y=mouseClick.getX()
    radius = random.randint(5, 20)
    print "x, y, radius", x, y, radius, "-->", x-radius, y-radius
    topL = graphics.Point(x-radius, y-radius)
    botR = graphics.Point(x+radius, y+radius)

#    return topL, botR

    ## test a return with a reasonable number 
    return x-0.01, y-0.03

def player1Move(win):
    topL, botR = player_common(win)

    ## there is no difference between the next two lines so you will get a
    ## point, not a line
    player1X = graphics.Line(graphics.Point(topL, topL), graphics.Point(botR, botR))
    player1X = graphics.Line(graphics.Point(topL, topL), graphics.Point(botR, botR))
    player1X.draw(win)
    
    
def player2Move(win):
    topL, botR = player_common(win)

    ## shouldn't there be a circle radius somewhere??
    player2Circle = graphics.Circle(topL, botR)
    player2Circle.draw(win)
    
if __name__ == "__main__":
    win = constructBoard()
    player1Win = False
    player2Win = False
    while player1Win == False and player2Win == False:
        player1Move(win)
        player2Move(win)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.