1

I want to make the word "CAT" into a button, so when it's clicked it says "CAT". Also, the button I want should be in the position that the word is right now when it's not a button. Any help provided is needed. Thank you

I have already tried the tkinter module, but the problem with that is it opens a separate window with the button. What I want is a button on the main screen.

import turtle
screen = turtle.Screen()

# this assures that the size of the screen will always be 400x400 ...
screen.setup(800,800)
turtle.ht()
turtle.penup()
turtle.goto (50, 200)
turtle.color("black")
turtle.write("CAT", move=False, align="center", font=("Times New Roman", 120, "bold"))
screen.bgpic("background.gif")
turtle.st()
turtle.forward(145)
turtle.left(90)
turtle.forward(10)
turtle.pendown()
turtle.forward(110)
turtle.left(90)
turtle.forward(287)
turtle.left(90)
turtle.forward(110)
turtle.left(90)
turtle.forward(287)
turtle.ht()

I expect the output to be a huge button (in black) at the top of the screen saying "CAT". When that button is pressed, I want it to say "CAT" out loud. Right now there is just text at the top saying "CAT". I want to replace that text with a button saying the same thing. If I use on screen click I want the click to be in specific coordinates. How would I do that. Thank You!

6
  • Possible duplicate of Python 3.0 using turtle.onclick Commented Jul 9, 2019 at 21:03
  • so how can I make the text a button?? Commented Jul 9, 2019 at 21:07
  • 1
    can't you draw rectangle which can look like button and put text on it. Using onscreenclick you can check if mouse clicked in rectangle. Commented Jul 9, 2019 at 21:08
  • I made the rectangle. How could I check if mouse clicked in the rectangle? Commented Jul 9, 2019 at 21:43
  • you can use onscreenclick(function_name) to run function_name(x,y) when you click mouse button. It gets position (x,y) of this click and you can check - ie. if 0 < x < 100 and 0 < y < 100: Commented Jul 9, 2019 at 21:47

2 Answers 2

1

We could take advantage of the object-oriented nature of turtle to define our own reusable button class for generating multiple buttons:

from turtle import Screen, Turtle

class Button(Turtle):
    FONT_NAME, FONT_SIZE, FONT_TYPE = 'Arial', 18, 'normal'

    HORIZONTAL_PAD, VERTICAL_PAD = 1.05, 1.15

    def __init__(self, text, position, command=None):
        super().__init__(visible=False)
        self.speed('fastest')
        self.penup()

        self.text = text
        self.position = position
        self.command = command

        self.width, self.height = self.drawButton()

    def drawButton(self):
        x, y = self.position
        self.setposition(x, y - self.FONT_SIZE/2)
        button_font = (self.FONT_NAME, self.FONT_SIZE, self.FONT_TYPE)
        self.write(self.text, align='center', move=True, font=button_font)

        width = 2 * (self.xcor() - x) * self.HORIZONTAL_PAD
        height = self.FONT_SIZE * self.VERTICAL_PAD

        self.setposition(x - width/2, y - height/2)
        self.pendown()

        for _ in range(2):
            self.forward(width)
            self.left(90)
            self.forward(height)
            self.left(90)

        self.penup()

        return width, height

    def clickButton(self, x, y):
        c_x, c_y = self.position
        half_w, half_h = self.width/2, self.height/2

        if c_x - half_w < x < c_x + half_w and c_y - half_h < y < c_y + half_h:
            (self.command)(x, y)

screen = Screen()

cyan = Button("Cyan Background", (100, 100), lambda x, y: screen.bgcolor('cyan'))

screen.onclick(cyan.clickButton, add=True)

yellow = Button("Yellow Background", (-100, -100), lambda x, y: screen.bgcolor('yellow'))

screen.onclick(yellow.clickButton, add=True)

screen.mainloop()

Embellish as you see fit.

Sign up to request clarification or add additional context in comments.

Comments

1

You could use turtle do draw rectangle which could look like button. And you can use onscreenclick(check_button) to run function check_button when you click screen. If you clicked in rectangle then it could run function which does something.

import turtle

def show_cat():
    turtle.ht()
    turtle.penup()
    turtle.goto (15, 220)
    turtle.color("black")
    turtle.write("CAT", move=False, align="center", font=("Times New Roman", 120, "bold"))

def check_button(x, y):
    if -300 < x < 300 and 200 < y < 400:
        show_cat()

screen = turtle.Screen()
screen.setup(800,800)

turtle.penup()
turtle.goto(-300, 200)
turtle.pendown()
turtle.begin_fill()
turtle.fillcolor('red')
turtle.fd(600)
turtle.left(90)
turtle.fd(300)
turtle.left(90)
turtle.fd(600)
turtle.left(90)
turtle.fd(300)
turtle.left(90)
turtle.end_fill()

turtle.onscreenclick(check_button)

turtle.mainloop()

Or you can use tk.Button with canvas.master as its parent, and put it on canvas using create_window(x, y, window=widget)

import turtle
import tkinter as tk

def show_cat():
    turtle.ht()
    turtle.penup()
    turtle.goto (15, 220)
    turtle.color("black")
    turtle.write("CAT", move=False, align="center", font=("Times New Roman", 120, "bold"))

screen = turtle.Screen()
screen.setup(800,800)

canvas = screen.getcanvas()

button = tk.Button(canvas.master, text="Click Me", command=show_cat)
canvas.create_window(0, 0, window=button)

#canvas.create_rectangle((100, 100, 700, 300))

turtle.mainloop()

The same way you can put other tkinter's widgets on canvas

EDIT: example with more widgets

import turtle
import tkinter as tk

def show_cat():
    label = tk.Label(canvas.master, text="Cat", font=("Times New Roman", 120, "bold"))
    canvas.create_window(0, -300, window=label)

    canvas.create_text(0, 300, text="HELLO", fill="red", font=("Times New Roman", 120, "bold"))

#-------------------------------------------

screen = turtle.Screen()
screen.setup(800,800)

canvas = screen.getcanvas()

button = tk.Button(canvas.master, text="Click Me", command=show_cat)
canvas.create_window(0, 0, window=button)

turtle.mainloop()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.