【问题标题】:Is there a way to implant tkinter input box into my turtle screen (to create paint)有没有办法将 tkinter 输入框植入我的海龟屏幕(创建绘画)
【发布时间】:2020-08-17 11:59:21
【问题描述】:

我的这段代码取自 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 中的海龟演示程序合并,该程序可以创建绘画..

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

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

【问题讨论】:

  • Turtle 基于 Tkinter,因此可以将其他 Tkinter 小部件嵌入到 Turtle 程序中。你只需要将myscreen._root 指定为master,或者在另一个Tkinter 窗口中使用myscreen._canvas

标签: python tkinter python-turtle


【解决方案1】:

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

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

否则,如果你不小心,你可能会得到两个根。这可能会导致进一步的错误,例如加载图像时出现无法解释的错误。

【讨论】:

  • 这些都只是选项。另一种选择:无需创建根;因为turtle在内部创建了一个,所以它之后的所有tkinter小部件都可以在没有明确指定root的情况下创建(并直接调用tk.mainloop()@end)。多个 Tk 根并没有错;不知道这个概念是从哪里来的(之前在堆栈溢出中看到过)。当多个 Tk 根使用不正确时会出现问题(在各种情况下自动隐式创建默认全局根,可能会导致这种情况)。
【解决方案2】:

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

  1. 选择@cdlane 的回答。它是最干净的,它使用 Turtle 的文档化界面,专为将其嵌入 Tkinter 应用程序而设计。
  2. 看看@volothud 回答的第二部分(第一部分如下所述)。
  3. 您只需将myscreen._root 指定为小部件的主人或在另一个Tkinter 窗口中使用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(...)(用于终端/命令行)与 GUI 一起使用?您可以改用tkinter.simpledialog(参见上面的代码 sn-p)。

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

【讨论】:

【解决方案3】:

看起来不错。只需合并您的 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()".

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-17
    • 1970-01-01
    • 2019-10-27
    • 2018-10-09
    • 1970-01-01
    相关资源
    最近更新 更多