Python. variable +=1 is not working as it should be in turtle
Question:
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.
Answers:
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)
When you call Button_a do this :
def button_a(value):
value += 1
pen.clear()
pen.write("a:{} b: {}".format(score_a, score_b), align="center", font=("Courier", 24, "normal"))
button_a(score_a)
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.
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)
When you call Button_a do this :
def button_a(value):
value += 1
pen.clear()
pen.write("a:{} b: {}".format(score_a, score_b), align="center", font=("Courier", 24, "normal"))
button_a(score_a)