I can't see the text on my screen

Joined
Feb 14, 2022
Messages
33
Reaction score
2
hi my name is kiaan and nine years old I am making my first app I am making the buttons I have programmed it to put the word "clicked"on the screen when I click on the button but when I do the word "clicked" does not show up and it does not give a SyntaxError to make it less confusing let me show the code
code:
import turtle

t = turtle.Turtle()
t.hideturtle()

for i in range(2):
t.forward(80)
t.left(90)
t.forward(30)
t.left(90)

t.penup()
t.goto(7, 6)
t.write("Start/Run", font=("Arial",12,"normal"))
def Button_click():
if Button_x<=x<=Button_xButtonlength:
if Button_y<=y<=Button_yButtonWidth:
print("clicked")


t.onclick(Button_click)

I am coding in python
 
Joined
Mar 3, 2021
Messages
240
Reaction score
30
I'm having a lot of trouble getting Turtle to work, so I'm just gonna have to guess. In your if blocks, you likely need to split the comparisons into two parts each and I think your logic is a bit off. You should be checking if the coords are between the start and the start plus the length.
Python:
def Button_click():
    if Button_x <= x and x <= Button_x + Button_xButtonlength:
        if Button_y <= y and y <= Button_y + Button_yButtonWidth:
            print("clicked")
 
Joined
Feb 14, 2022
Messages
33
Reaction score
2
I'm having a lot of trouble getting Turtle to work, so I'm just gonna have to guess. In your if blocks, you likely need to split the comparisons into two parts each and I think your logic is a bit off. You should be checking if the coords are between the start and the start plus the length.
Python:
def Button_click():
    if Button_x <= x and x <= Button_x + Button_xButtonlength:
        if Button_y <= y and y <= Button_y + Button_yButtonWidth:
            print("clicked")
i am sorry i do not understand what you are saying
 
Joined
Mar 3, 2021
Messages
240
Reaction score
30
Which part? Did you try switching your Button_click with the one I posted? I can't verify it on my own.
 
Joined
Mar 3, 2021
Messages
240
Reaction score
30
I'm unclear how to help. Please be more descriptive of what you do and don't understand and what, if anything, that you tried after my first post.
 
Joined
Feb 14, 2022
Messages
33
Reaction score
2
but i did understand your second post his one
post:
Which part? Did you try switching your Button_click with the one I posted? I can't verify it on my own.
 
Joined
Mar 3, 2021
Messages
240
Reaction score
30
I finally got Turtle working. My mistake was naming my script turtle.py. It appears that you're logic was flawed, you're not specifying the button start and sizes and you're adding the click event to the wrong place.

So, from the top. Your if blocks, the comparisons, need to be split into two parts each. You can' t compare one number to multiple others at a time.

Second, you should add the Button_x and Button_y to the length and widths that you're comparing it to. Fortunately for your button, they're zero anyway, so it'll work without that.

Last, you're calling onclick on the Turtle object, so it's waiting for you to click the turtle (which you can't do, because it's hidden). You need to add your onclick event to the screen itself so that it's called whenever the user clicks anywhere on the screen.

Below is a fully working example.

Python:
#!/usr/bin/env python3

import turtle

t = turtle.Turtle()
t.hideturtle()

s = turtle.Screen()

for i in range(2):
    t.forward(80)
    t.left(90)
    t.forward(30)
    t.left(90)

Button_x = 0
Button_y = 0
Button_xButtonlength = 80
Button_yButtonWidth = 30

t.penup()
t.goto(7, 6)
t.write("Start/Run", font=("Arial",12,"normal"))
def Button_click(x, y):
    if Button_x <= x and x <= Button_x + Button_xButtonlength:
        if Button_y <= y and y <= Button_y + Button_yButtonWidth:
            print("clicked")

s.onclick(Button_click)
s.mainloop()
 
Joined
Mar 3, 2021
Messages
240
Reaction score
30
Check your capitalization, maybe? The function name and what is given to onclick need to be identical.
 
Joined
Mar 3, 2021
Messages
240
Reaction score
30
No worries! I'm glad we got through it. Just come on back and make a new thread if anything else comes up. And keep at it! Code is great.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,039
Messages
2,570,376
Members
47,028
Latest member
IsmaelLans

Latest Threads

Top