有没有办法将 tkinter 输入框植入我的乌龟屏幕(创建绘画)

Is there a way to implant tkinter input box into my turtle screen (to create paint)

我从 trinket.io

中获取了这段代码
import turtle

tina = turtle.Turtle()

tina.shape("turtle")

# promt user for color and make turtle that color
turtle_color = input("What color should tina the turtle be?")

tina.color(turtle_color)

# promt user for background color and makes it that color

myscreen = turtle.Screen()
background_color = input("What color should background be?")
myscreen.bgcolor(background_color)   

我想做的是将我的 tkinter 输入框合并到程序的一侧并创建一种类似绘画的程序

这是 tkinter 按钮的代码:

from tkinter import *

master = Tk()
e = Entry(master)
e.pack()

e.focus_set()

def callback():
    print e.get() # This is the text you may want to use later

b = Button(master, text = "OK", width = 10, command = callback)
b.pack()

mainloop()

我们还可以将它与 python 中的 turtle 演示程序合并,后者可以创建绘画..

所以我只想知道如何合并它们

通过合并我的意思是海龟一侧的 tkinter 按钮和输入框 答案仍然被接受..谢谢

好像还不错。只需合并您的 turtle 和 tkinter 代码,例如:

import tkinter as tk
import turtle

# Setup turtle

tina = turtle.Turtle()
tina.shape("turtle")
tina.color('red')
myscreen = turtle.Screen()
myscreen.bgcolor('black')

# Setup GUI

# Use independent Tk root:
#root = tk.Tk()
# Use the same Tk root with turtle:
assert isinstance(tina.screen._root, tk.Tk)  # True
root = tina.screen._root

distVar = tk.IntVar(root)
distEntry = tk.Entry(root, textvariable=distVar)
distEntry.pack()
distEntry.focus_set()

def moveFwd():
  tina.forward(distVar.get())
fwdBtn = tk.Button(root, text='MoveFwd', command=moveFwd)
fwdBtn.pack()

# After setup is done (turtle, widgets & event-callbacks),
# enter event-loop (&react to user input in event-callbacks)
root.mainloop()

只是为了让人们停止引用原始示例作为“坏”,
这是一个更简单的:
(同样,这些都只是选项(对于一个简单的问题)。)

import tkinter as tk
import turtle

# Setup turtle.
# This will create a Tk root, that can be implicitly reused.
t = turtle.Turtle()

# Setup tkinter GUI.

distVar = tk.IntVar()
distEntry = tk.Entry(textvariable=distVar)
distEntry.pack()
distEntry.focus_set()

def moveFwd(): t.forward(distVar.get())
fwdBtn = tk.Button(text='MoveFwd', command=moveFwd)
fwdBtn.pack()

# After setup is done (turtle, widgets & event-callbacks),
# enter event-loop (&react to user input in event-callbacks)
tk.mainloop()  # Also known as "turtle.done()".

Turtle 基于 Tkinter,因此有一种方法可以将其他 Tkinter 小部件嵌入到 Turtle 程序中。您可以通过多种方式做到这一点:

  1. 使用@cdlane 的回答。它是最干净的一个,它使用 Turtle 的文档接口,专门设计用于将它嵌入到 Tkinter 应用程序中。
  2. 看看@volothud 回答的第二部分(第一部分在下面描述)。
  3. 您只需要将 myscreen._root 指定为小部件的主控或在另一个 Tkinter window.
  4. 中使用 myscreen._canvas

这是第三个选项的示例:

import tkinter as tk
from tkinter.simpledialog import askstring
import turtle


tina = turtle.Turtle()
tina.shape("turtle")

screen = turtle.Screen()
root = screen._root

controls = tk.Frame(root)
tk.Label(controls, text="Move forward:").pack(side=tk.LEFT)
fwd_entry = tk.Entry(controls)
fwd_entry.pack(side=tk.LEFT)
tk.Button(controls, text="Go!", command=lambda: tina.forward(int(fwd_entry.get()))).pack(side=tk.LEFT)
controls.pack()

tina_color = askstring("Tina's color", "What color should Tina the turtle be?")
bg_color = askstring("The background color", "What color should the background be?")
tina.color(tina_color)
screen.bgcolor(bg_color)

root.mainloop()

注释 1:为什么要将 input(...)(用于 terminal/command-line)与 GUI 一起使用?您可以改用 tkinter.simpledialog(请参阅上面的代码片段)。

注意 2:输入未经验证,因此用户可以输入任何内容(您可以使用 try/except 捕获它们并使用 [=37= 显示错误对话框]).

虽然到目前为止您收到的答案有效,但我认为它们是反向组装的,如果不是错误的话。提示是他们使用了未记录的接口:

root = screen._root
assert isinstance(tina.screen._root, tk.Tk)

Turtle 知道 它是在 tkinter 中实现的,与标准 standalone 接口一起,它记录了一个 embedded 将 turtle 与 tkinter 混合时的界面。修改@volothud 的示例:

import tkinter as tk
from tkinter.simpledialog import askstring
from turtle import TurtleScreen, RawTurtle

root = tk.Tk()

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

distance = tk.IntVar()

controls = tk.Frame(root)
tk.Label(controls, text="Move forward:").pack(side=tk.LEFT)
tk.Entry(controls, textvariable=distance).pack(side=tk.LEFT)
tk.Button(controls, text="Go!", command=lambda: turtle.forward(distance.get())).pack(side=tk.LEFT)
controls.pack()

turtle_color = askstring("Turtle's color", "What color should the turtle be?")
background_color = askstring("Background color", "What color should the background be?")

screen = TurtleScreen(canvas)
screen.bgcolor(background_color)

turtle = RawTurtle(screen)
turtle.shape('turtle')
turtle.color(turtle_color)

root.mainloop()

否则,一不小心,可能会得到两个根。这可能会在以后导致错误,例如加载图像时无法解释的错误。