Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python. variable +=1 is not working as it should be in turtle

I’m trying to make something like a "clicker" on buttons for 2 person’s, but my variable +=1 is not working in this program. My code:

from tkinter import *
import turtle
wn = turtle.Screen()
wn.title("Pong by Daniel")
wn.bgcolor("black")
wn.setup(width=800, height=600)
wn.tracer(0)
score_a = 0
score_b = 0

pen = turtle.Turtle()
pen.speed(0)
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 240)
pen.write("a:0  b: 0", align="center", font=("Courier", 24, "normal"))
     
button_a = Button( text="a")
button_a.place(x=530, y=300)
button_b = Button( text="b")
button_b.place(x=130, y=300)

def button_a():
     score_a+= 1
     pen.clear()
     pen.write("a:{}  b: {}".format(score_a, score_b), align="center", font=("Courier", 24, "normal"))
def button_b():
     score_b += 1
     pen.clear()
     pen.write("a:{}  b: {}".format(score_a, score_b), align="center", font=("Courier", 24, "normal"))

     
while True:
     wn.update()    
     turtle.onclick(button_a)
     turtle.onclick(button_b)


i cant put those variables in def, bcs it would reset all the time(i think), somebody have any idea, opinion on this? Im new in programming, and need to learn a lot.

I have tried using it without def, but i need it when im using turtle.onclick, i dont know how to move from this point.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

there is nothing wrong with variable += 1. Its not working because of scope issue and not defind commend to buttons. Try this:

from tkinter import *
import turtle

wn = turtle.Screen()
wn.title("Pong by Daniel")
wn.bgcolor("black")
wn.setup(width=800, height=600)
wn.tracer(0)
score_a = 0
score_b = 0

pen = turtle.Turtle()
pen.speed(0)
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 240)
pen.write("a:0  b: 0", align="center", font=("Courier", 24, "normal"))



def button_a_1():
   print("hello")
   global score_a
   score_a += 1
   pen.clear()
   pen.write("a:{}  b: {}".format(score_a, score_b), 
   align="center", font=("Courier", 24, "normal"))


def button_b_1():
   global score_b
   score_b += 1
   pen.clear()
   pen.write("a:{}  b: {}".format(score_a, score_b), 
   align="center", font=("Courier", 24, "normal"))


button_a = Button(text="a", command=button_a_1)
button_a.place(x=530, y=300)
button_b = Button(text="b", command=button_b_1)
button_b.place(x=130, y=300)

while True:
   wn.update()
   turtle.onclick(button_a_1)
   turtle.onclick(button_b_1)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading