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

时间:2018-05-29 13:55:25

标签: python tkinter turtle-graphics

我一直在尝试改进.Entry命令并使用用户输入放入乌龟,但我一直收到错误:

就我而言,我正在尝试向用户询问他们想要在乌龟中使用的颜色。

我的代码:

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()

1 个答案:

答案 0 :(得分:0)

我的建议是你完全在龟中工作而不是下降到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)的更多信息,以了解其功能。您还需要阅读有关乌龟的更多信息,因为嵌入在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()