结合 python turtle 和 tkinter`

Combining python turtle and tkinter`

我需要结合 python turtle 和 tkinter 来玩井字游戏,我查看了一些网站并尝试过,但它没有像我想象的那样工作,所以我需要帮助。 代码:

from turtle import *
from circle import Tac
from cross import Tic
from tkinter import *

screen = Screen()
screen.title("Tic Tac Toe")
screen.setup(width=300, height=500)
tim = Turtle()
board = "board.gif"
screen.addshape(board)
img_turtle = Turtle()
img_turtle.shape(board)
screen.listen()
tim.penup()
tim.seth(0)

我要添加的代码:

button1 = Button(text="button", command=tic.one)
button1.place(x=-70, y=42)

Python turtle 设计用于在更大的 tkinter 程序中运行 standaloneembedded。您正在尝试在嵌入式情况下使用独立界面。以下是我对您的代码的预期外观的返工。由于您没有提供可运行的示例,因此这是一个不完整的草图:

from tkinter import *
from turtle import RawTurtle, TurtleScreen
from circle import Tac
from cross import Tic

BOARD = "board.gif"

root = Tk()
root.title("Tic Tac Toe")

canvas = Canvas(root, width=300, height=500)
canvas.pack(side=LEFT)

screen = TurtleScreen(canvas)
screen.addshape(BOARD)

tim = RawTurtle(screen)
tim.penup()

img_turtle = RawTurtle(screen)
img_turtle.shape(BOARD)

button = Button(root, text="button", command=tic.one)
button.pack()

screen.mainloop()