【问题标题】:Trying to take user input and make a turtle dot尝试接受用户输入并制作海龟点
【发布时间】:2019-11-13 23:52:02
【问题描述】:

在这种情况下,尝试获取用户输入的数字,并让海龟用我们从用户输入中获得的数字制作一个点。这是我正在尝试做的一个学校项目,我尝试找到他们确实没有帮助的 YouTube 视频。

from tkinter import *
import tkinter
import turtle

wn = turtle.Screen()
wn.bgcolor('black')

player = turtle.Turtle()
player.shape('turtle')
player.color('white')

def sad():
    player.dot(str(kj.get()))

top = tkinter.Tk()

top.geometry('600x600')

kj = Entry(top, bd =5)
kj.pack()

B = tkinter.Button(top, text ="Hello", command = sad)
B.pack()

wn.mainloop()
top.mainloop()

【问题讨论】:

  • 没有理由导入一个模块,然后导入该模块的所有成员。

标签: python tkinter turtle-graphics


【解决方案1】:

当结合 tkinter 使用 Python turtle 时,您需要使用 embedded turtle 方法,而不是 standalone 您使用的方法。由于turtle 是在tkinter 之上构建的,因此您有效地创建了两个根,最终会遇到麻烦。 (例如,图像可能不适合您。)当您同时调用 top.mainloop()wn.mainloop() 时,您显然对组合感到困惑!

这是一个如何在 tkinter 中为您的程序嵌入海龟的示例:

import tkinter as tk
from turtle import TurtleScreen, RawTurtle

def set_position():
    player.setposition(x_entry.get(), y_entry.get())
    player.dot(30, 'blue')
    player.home()

top = tk.Tk()

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

screen = TurtleScreen(canvas)
screen.bgcolor('black')

player = RawTurtle(screen)
player.shape('turtle')
player.color('red', 'white')
player.penup()

x_entry = tk.DoubleVar()
tk.Label(top, text="X: ").pack(side=tk.LEFT)
tk.Entry(top, textvariable=x_entry).pack(side=tk.LEFT)

y_entry = tk.DoubleVar()
tk.Label(top, text="Y: ").pack(side=tk.LEFT)
tk.Entry(top, textvariable=y_entry).pack(side=tk.LEFT)

tk.Button(top, text="Draw Dot!", command=set_position).pack()

screen.mainloop()

我的建议是:首先,如果可以的话,尽量在独立 turtle 内工作,除非你真的需要,否则不要引入 tkinter;其次,不要相信任何会在 tkinter 环境中尝试使用 standalone turtle 类 embedded 的错误答案。

【讨论】:

    【解决方案2】:

    使用turtle,您可以使用setPos 命令告诉它去某个位置。如果您只是将用户输入的值转换为坐标,请告诉您海龟去那里然后开始绘图。

    这是一个解决方案:

    import turtle
    from time import sleep
    from tkinter import *
    
    #Setup
    root=Tk()
    
    wn = turtle.Screen()
    wn.bgcolor('black')
    
    player = turtle.Turtle()
    player.shape('turtle')
    player.color('white')
    
    def goToLocation(coords):
        #Get user input and split it into two different coordinants (coordsx and coordsy)
    
        coordsx, coordsy = coords.split(" ")
    
        #Set turtles position to the coords specified
    
        player.hideturtle()
        player.penup()
        player.setpos(int(coordsx),int(coordsy))
    
        #Draw the circle of size
        player.pensize(50)
        player.showturtle()
        player.pendown()
        player.forward(1)
    
        sleep(5)
    
    
    #Button clicked handler
    def retrieve_input():
        inputValue=textBox.get("1.0","end-1c")
        print(inputValue)
    
        #Calls the previous function
        goToLocation(inputValue)
    
    
    #Input box setup
    textBox=Text(root, height=2, width=10)
    textBox.pack()
    buttonCommit=Button(root, height=1, width=10, text="Commit", 
                    command=lambda: retrieve_input())
    #command=lambda: retrieve_input() >>> just means do this when i press the button
    
    buttonCommit.pack()
    
    mainloop()
    

    【讨论】:

    • Toby- 在第 18、41、48 行弹出错误
    • 您是否已将整个代码块复制到您的代码文件中?这应该替换您现有的代码 - 确保您输入的坐标为“xx yy”,不带引号。
    • 为什么是command=lambda: retrieve_input() 而不是简单的command=retrieve_input
    • cdlane 很抱歉回复晚了 - 我只是根据我看到的 tkinter 文档来回答,因为我没有太多经验。
    猜你喜欢
    • 2015-05-18
    • 2017-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多