Как мне получить данные из tkinter и добавить их в Turtle? - PullRequest
0 голосов
/ 29 мая 2018

Я пытался создать команду. Введите команду и используйте ввод пользователя для ввода черепахи, но я получаю сообщение об ошибке:

В моем случае я пытаюсь попросить у пользователя цветчто они хотят использовать в черепахе.

Мой код:

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 голосов
/ 29 мая 2018

Я рекомендую вам полностью работать в 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()
...