有没有办法将tkinter输入框植入到我的乌龟屏幕中创建颜料

问题描述

我将此代码取自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按钮和乌龟一侧的输入框 答案仍然被接受..谢谢

解决方法

尽管到目前为止您已经收到了有效的答案,但我认为这些答案是向后汇编的,即使不是正确的。提示是他们使用了未公开的界面:

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代码,例如:

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',# 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窗口中使用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,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(请参阅上面的代码)。

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...