Python 如何从tkinter获取数据并将其添加到turtle中?

Python 如何从tkinter获取数据并将其添加到turtle中?,python,tkinter,turtle-graphics,Python,Tkinter,Turtle Graphics,我一直在尝试使用tkinters.Entry命令并使用用户输入输入到turtle中,但我一直遇到一个错误: 在我的例子中,我试图要求用户提供一种他们想在海龟身上使用的颜色 我的代码: import tkinter from turtle import Turtle #Create and Format window w = tkinter.Tk() w.title("Getting To Know You") w.geometry("400x200") #Favorite Color

我一直在尝试使用tkinters.Entry命令并使用用户输入输入到turtle中,但我一直遇到一个错误:

在我的例子中,我试图要求用户提供一种他们想在海龟身上使用的颜色

我的代码:

import tkinter
from turtle import Turtle

#Create and Format window
w = tkinter.Tk()
w.title("Getting To Know You")
w.geometry("400x200")


#Favorite Color


lbl3= tkinter.Label(w, text = "What's your favorite color?")
lbl3.grid(row = 10 , column = 2)

olor = tkinter.Entry(w)
olor.grid(row = 12, column = 2)

t = Turtle()
t.begin_fill()
t.color(olor)
shape = int (input ("What is your favorite shape?"))

w.mainloop()

我建议您完全在turtle内部工作,而不是降低到tkinter级别:

from turtle import Turtle, Screen

# Create and Format window
screen = Screen()
screen.setup(400, 200)
screen.title("Getting To Know You")

# Favorite Color

color = screen.textinput("Choose a color", "What's your favorite color?")

turtle = Turtle()
turtle.color(color)

turtle.begin_fill()
turtle.circle(25)
turtle.end_fill()

turtle.hideturtle()
screen.mainloop()
如果必须从tkinter执行此操作,则需要阅读更多关于tkinter元素的信息,如
Entry
,以了解它们的功能。您还需要了解更多关于turtle的信息,因为它在嵌入tkinter窗口时的调用方式不同。以下是您尝试做的大致情况:

import tkinter as tk
from turtle import RawTurtle, TurtleScreen, ScrolledCanvas

root = tk.Tk()
root.geometry("400x200")
root.title("Getting To Know You")

def draw_circle():
    turtle.color(color_entry.get())

    turtle.begin_fill()
    turtle.circle(25)
    turtle.end_fill()

# Favorite Color

tk.Label(root, text="What's your favorite color?").pack()

color_entry = tk.Entry(root)
color_entry.pack()

tk.Button(root, text='Draw Circle', command=draw_circle).pack()

canvas = ScrolledCanvas(root)
canvas.pack(fill=tk.BOTH, expand=tk.YES)

screen = TurtleScreen(canvas)
turtle = RawTurtle(screen, visible=False)

screen.mainloop()

Entry是一个Entry对象。要从中获取字符串,您需要运行Entry.get(),但我需要更多的代码来给出一个代码作为答案。好的,如果您愿意,我可以发布我的代码。请这样做,或者使用get()自行修复代码,这样您就可以看到困难:我不知道出了什么问题,我们不需要查看所有代码。我们需要看电影。你已经发布了很多与所问问题无关的代码。因此,想做一些类似的工作^^^^^^^非常感谢,我发现这非常有用。