I am having trouble getting my button to work. What I am trying to do is when the button is pressed, it should call the function clickGame() The window is opening after I press the button but I get an error message:

self.tk = master.tk
AttributeError: 'Point' object has no attribute 'tk'

I don't understand what the error means. Please help! Thanks in advance.

from graphics import*
from random import randint
import random
from tkinter import *
from tkinter import ttk

def clickGame():

    win = GraphWin("Circle Game", 800, 800)
    win.setCoords(-100, -100, 100, 100)
    win.setBackground("white")

    center = Text(Point(10,80), "Click to create a circle")   
    center.draw(win)
    win.getMouse() # waits for mousclick to get score

    i = 50
    score = 0
    for i in range(20):

        p = win.getMouse()
        x = p.getX()
        y = p.getY()   

        color = ("magenta","skyblue","purple","pink", "green", "violet", "cyan")
        color = random.choice(color)

        if color == "magenta":
            score = score + 10

        elif color == "skyblue":
            score = score + 10

        elif color == "purple":
            score = score + 10

        elif color == "pink":
            score = score + 10

        elif color == "green":
            score = score + 10

        elif color == "violet":
            score = score + 10

        elif color == "cyan":
            score = score + 10

        # draw score on screen
        # score overlaps

        points = Text(Point(10,90), "Score: %s" %score) 
        points.draw(win)

        circle = Circle(Point(x, y), randint(10, 40))
        circle.setFill(color)
        circle.draw(win)

def getButton():
    root = Tk()

    root.title("MainMenu")
    ttk.Button(root, text = "ClickGame", command = clickGame).grid()
##    button1 = Button(root, text = "clickGame", command = clickGame)
##    button1.pack()
    root.mainloop()    

def main():

    getButton()

main()

The problem is that you write from tkinter import * after from graphics import *. Some objects defined in the graphics.py package get shadowed by objects defined in the tkinter package. In this case, the graphics.py Text class is shadowed by the tkinter Text widget. The best thing to do is to avoid the import * idiom, especially with tkinter. You can do

import tkinter as tk

...

root = tk.Tk()

The code works this way. It would be a good idea to add a button to kill the window and exit the game, because I had to kill the process from the outside.

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.