【问题标题】:How to allow users to input data inside a turtle screen window如何允许用户在海龟屏幕窗口中输入数据
【发布时间】:2020-05-15 02:53:30
【问题描述】:

我正在使用 Python 的 turtle 模块创建一个计算器。 我需要用户在窗口中输入信息,而不是在运行程序时弹出的命令行界面中输入信息,然后将信息打印到屏幕上 有点像如何在命令提示符中输入信息,而无需将信息放在辅助窗口中。

import turtle
import pyglet
import time

def pen_message(message = "", font = "", font_size = 0, loc_x = 0, loc_y = 0):
    pen = turtle.Turtle()
    pen.speed(0)
    pen.penup()
    pen.color("sky blue")
    pen.goto(loc_x, loc_y)

    pen.write(message, align = "center", font = (font, font_size, "normal"))

def main():

    # Main program window
    win = turtle.Screen()
    win.cv._rootwindow.resizable(False, False)
    win.screensize()
    win.setup(width = 1.0, height = 1.0)
    win.title("Geometric & Algabraic Calculator by @_c0d3_x_")
    win.bgcolor("black")
    win.setup(width = 800, height = 600)
    win.tracer(0)

    # Pen for text

    title = pen_message("Geometric & Algabraic Calculator", "Tech Noir", 15, 0, 200)

    msg = pen_message("Welcome User", "Courier", 15, 0, 150)

    userInput = pen_message(input("Your name: "), "Courier", 15, 0, 100)



main()

这就是我试图避免的 该程序使用户在正确的屏幕上输入数据,而不是让他们在实际窗口本身中输入数据。 我将如何实现这一目标?

【问题讨论】:

    标签: python oop user-interface input turtle-graphics


    【解决方案1】:

    虽然不是在海龟窗口本身而不是控制台上输入,但可以使用海龟提供的两种输入法通过弹出窗口获得用户输入:

    textinput(title, prompt)
    
    numinput(title, prompt, default=None, minval=None, maxval=None)
    

    这些旨在防止控制台样式输入需要捕获的一些错误。它们是在 Python 3 中引入的,在 Python 2 中不可用。

    如果您在代码中使用listen() 来启用键盘事件,则需要在调用上述命令后重做listen() 命令,因为它们会成为侦听器

    【讨论】:

    • 感谢您的回复!在您的帮助下,我现在有了更多的了解,并进行了一些研究,并找到了 python 的文档页面,其中包含模块输入法的说明!
    【解决方案2】:

    更新:感谢 cdlane 的帮助,我能够对 textinput() 方法进行更多研究,并找到该方法本身的 Python 文档页面,这里是将来可能需要它的任何人的链接:

    [1]:https://docs.python.org/3.1/library/turtle.html#turtle.textinput

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-18
      • 1970-01-01
      • 2017-10-01
      • 1970-01-01
      • 2021-02-23
      • 2021-02-05
      • 2020-01-03
      • 2016-03-06
      相关资源
      最近更新 更多