Python Turtle 对象与 tkinter 结合可能吗?

发布于 2025-01-09 16:24:12 字数 1089 浏览 0 评论 0原文

当我运行 main.py 时遇到问题,它显示两个屏幕。我知道这是因为我正在调用一个对象,只是不确定是否可以使用对象并将其显示在 tkinter 窗口上。

ma​​in.py

import tkinter as tk
from turtle import TurtleScreen
from createDotTest import Dot
top = tk.Tk()

canvas = tk.Canvas(top, width=600, height=600)
canvas.pack()

screen = TurtleScreen(canvas) 
screen.tracer(0)

list_of_points = []
list_of_lines = []

for x in range(100):
    dot = Dot()
    list_of_points.append(dot)

screen.mainloop()

createDotTest.py

from turtle import Turtle
import random

class Dot(Turtle):
    def __init__(self):
        super().__init__()
        x_pos = random.randint(-280, 280)
        y_pos = random.randint(-280, 280)
        self.shapesize(.2,.2)
        self.penup()
        self.shape('circle')
        self.goto(x_pos, y_pos)
        self.stamp()

我也尝试在 createDotTest.py 中将 Turtle 替换为 TurtleRaw,但我得到

raise TurtleGraphicsError("bad canvas argument %s" % canvas)
turtle.TurtleGraphicsError: bad canvas argument None

I'm having an issue when I run my main.py, it shows two screens. I know it's because I'm calling an object, just not sure if it's possible to use objects and have it show up on the tkinter window.

main.py

import tkinter as tk
from turtle import TurtleScreen
from createDotTest import Dot
top = tk.Tk()

canvas = tk.Canvas(top, width=600, height=600)
canvas.pack()

screen = TurtleScreen(canvas) 
screen.tracer(0)

list_of_points = []
list_of_lines = []

for x in range(100):
    dot = Dot()
    list_of_points.append(dot)

screen.mainloop()

createDotTest.py

from turtle import Turtle
import random

class Dot(Turtle):
    def __init__(self):
        super().__init__()
        x_pos = random.randint(-280, 280)
        y_pos = random.randint(-280, 280)
        self.shapesize(.2,.2)
        self.penup()
        self.shape('circle')
        self.goto(x_pos, y_pos)
        self.stamp()

I tried also replacing Turtle as TurtleRaw in the createDotTest.py but I get

raise TurtleGraphicsError("bad canvas argument %s" % canvas)
turtle.TurtleGraphicsError: bad canvas argument None

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吻风 2025-01-16 16:24:12

这个问题看起来类似于结合tkinter和turtle

我不确定,但这可能帮助你

This question looks similar to Combine tkinter and turtle

I am not sure but this may help you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文