Home > Back-end >  Goto function isn't working in rawturtle with mixed turtle and tkinter
Goto function isn't working in rawturtle with mixed turtle and tkinter

Time:02-28

I am trying to make tic tac toe project in mixed turtle and tkinter and it is going very well by now, I have a bug now. I made 9 buttons for every field with coordinates, but whenever I click any button it goes to the first written position.

from tkinter import *
from turtle import *

BOARD = "board.gif"


root = Tk()
canvas = Canvas(root, width=300, height=500)
canvas.grid(padx=2, pady=2, row=0, column=0, rowspan=10, columnspan=10)

screen = TurtleScreen(canvas)

tim = RawTurtle(canvas)

draw = RawTurtle(canvas)
screen.addshape(BOARD)
img_turtle = RawTurtle(screen)
img_turtle.shape(BOARD)

def ticturtle():
    tim.color("black")
    tim.speed("fastest")
    tim.hideturtle()
    tim.pendown()
    tim.begin_fill()
    tim.left(315)
    for i in range(2):
        tim.forward(13)
        tim.left(90)
        tim.forward(26)
        tim.right(90)
        tim.forward(26)
        tim.left(90)
        tim.forward(13)
        tim.right(270)
        tim.forward(26)
        tim.left(270)
        tim.forward(26)
        tim.left(90)
    tim.end_fill()
    tim.penup()
    tim.seth(0)

class Tic():
    def one(self):
        tim.penup()
        tim.setpos(-95, 42)
        ticturtle()

    def two(self):
        tim.penup()
        tim.setpos(-25, 42)
        ticturtle()

    def three(self):
        tim.penup()
        tim.setpos(45, 42)
        ticturtle()

    def four(self):
        tim.penup()
        tim.goto(-95, -22)
        ticturtle()

    def five(self):
        tim.penup()
        tim.goto(-25, -22)
        ticturtle()

    def six(self):
        tim.penup()
        tim.goto(45, -22)
        ticturtle()

    def seven(self):
        tim.penup()
        tim.goto(-95, -85)
        ticturtle()

    def eight(self):
        tim.penup()
        tim.goto(-25, -85)
        ticturtle()

    def nine(self):
        tim.penup()
        tim.goto(45, -85)
        ticturtle()

def tacturtle():
    tim.pendown()
    tim.hideturtle()
    tim.color("red")
    tim.pensize(7)
    tim.speed("fastest")
    tim.circle(25)
    tim.shapesize(12)
    tim.penup()

class Tac():

    def I(self):
        tim.penup()
        tim.setpos(-70, 42)
        tacturtle()

    def II(self):
        tim.penup()
        tim.setpos(0, 42)
        tacturtle()

    def III(self):
        tim.penup()
        tim.setpos(70, 42)
        tacturtle()

    def IV(self):
        tim.penup()
        tim.goto(-70, -22)
        tacturtle()

    def V(self):
        tim.penup()
        tim.goto(0, -22)
        tacturtle()

    def VI(self):
        tim.penup()
        tim.goto(70, -22)
        tacturtle()

    def VII(self):
        tim.penup()
        tim.goto(-70, -85)
        tacturtle()

    def VIII(self):
        tim.penup()
        tim.goto(0, -85)
        tacturtle()

    def IX(self):
        tim.penup()
        tim.goto(70, -82)
        tacturtle()

tic = Tic()
tac = Tac()

x = 0

def tic_or_tac1():
    global x
    if x % 2 == 0:
        tic.one()
        x  = 1
    else:
        tac.I()
        x  = 1

def tic_or_tac2():
    global x
    if x % 2 == 0:
        tic.two()
    else:
        tac.II()
    x  = 1

def tic_or_tac3():
    global x
    if x % 2 == 0:
        tic.three()
    else:
        tac.III()
    x  = 1

def tic_or_tac4():
    global x
    if x % 2 == 0:
        tic.four()
    else:
        tac.IV()
    x  = 1

def tic_or_tac5():
    global x
    if x % 2 == 0:
        tic.five()
    else:
        tac.V()
    x  = 1

def tic_or_tac6():
    global x
    if x % 2 == 0:
        tic.six()
    else:
        tac.VI()
    x  = 1

def tic_or_tac7():
    global x
    if x % 2 == 0:
        tic.seven()
    else:
        tac.VII()
    x  = 1

def tic_or_tac8():
    global x
    if x % 2 == 0:
        tic.eight()
    else:
        tac.VIII()
    x  = 1

def tic_or_tac9():
    global x
    if x % 2 == 0:
        tic.nine()
    else:
        tac.IX()

    x  = 1


def hide_me(event):
    event.widget.grid_forget()

def callback(Buttons):
   Buttons.bind('<Button>', hide_me)


def callback_and_hide(button):
    callback(Play_Button1)
    button.place_forget()
    tic_or_tac1()

def callback_and_hide2(button):
    callback(Play_Button2)
    button.place_forget()
    tic_or_tac2()

def callback_and_hide3(button):
    callback(Play_Button3)
    button.place_forget()
    tic_or_tac3()

def callback_and_hide4(button):
    callback(Play_Button4)
    button.place_forget()
    tic_or_tac4()

def callback_and_hide5(button):
    callback(Play_Button5)
    button.place_forget()
    tic_or_tac5()

def callback_and_hide6(button):
    callback(Play_Button6)
    button.place_forget()
    tic_or_tac6()

def callback_and_hide7(button):
    callback(Play_Button7)
    button.place_forget()
    tic_or_tac7()

def callback_and_hide8(button):
    callback(Play_Button8)
    button.place_forget()
    tic_or_tac3()

def callback_and_hide9(button):
    callback(Play_Button9)
    button.place_forget()
    tic_or_tac9()

Play_Button1 = Button(master = root,text='ㅤㅤ',command=lambda: callback_and_hide(Play_Button1))
Play_Button1.config(bg="white",fg="black")
Play_Button1.place(x=54, y=157, width=61, height=61)

Play_Button2 = Button(master = root, text ="ㅤㅤ", command=lambda: callback_and_hide(Play_Button2))
Play_Button2.config(bg="white",fg="black")
Play_Button2.place(x=124, y=157, width=61, height=61)

Play_Button3 = Button(master = root, text ="ㅤㅤ", command=lambda: callback_and_hide(Play_Button3))
Play_Button3.config(bg="white",fg="black")
Play_Button3.place(x=194, y=157, width=61, height=61)

Play_Button4 = Button(master = root, text ="ㅤㅤ", command=lambda: callback_and_hide(Play_Button4))
Play_Button4.config(bg="white",fg="black")
Play_Button4.place(x=54, y=227, width=61, height=61)

Play_Button5 = Button(master = root, text ="ㅤㅤ", command=lambda: callback_and_hide(Play_Button5))
Play_Button5.config(bg="white",fg="black")
Play_Button5.place(x=124, y=227, width=61, height=61)

Play_Button6 = Button(master = root, text ="ㅤㅤ", command=lambda: callback_and_hide(Play_Button6))
Play_Button6.config(bg="white",fg="black")
Play_Button6.place(x=194, y=227, width=61, height=61)

Play_Button7 = Button(master = root, text ="ㅤㅤ", command=lambda: callback_and_hide(Play_Button7))
Play_Button7.config(bg="white",fg="black")
Play_Button7.place(x=54, y=293, width=61, height=61)

Play_Button8 = Button(master = root, text ="ㅤㅤ", command=lambda: callback_and_hide(Play_Button8))
Play_Button8.config(bg="white",fg="black")
Play_Button8.place(x=124, y=293, width=61, height=61)

Play_Button9 = Button(master = root, text ="ㅤㅤ", command=lambda: callback_and_hide(Play_Button9))
Play_Button9.config(bg="white",fg="black")
Play_Button9.place(x=194, y=293, width=61, height=61)

screen.mainloop()
 

‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎

CodePudding user response:

You have problem because all Buttons run the same callback_and_hide but every Button should run diferent function callback_and_hide2, callback_and_hide3 , etc. That's all.

  • Related