How to allow users to input data inside a turtle screen window

Viewed 563

I am creating a calculator using the turtle module of Python. I need the user to input information inside the window, as opposed to entering info inside the command line interface that pops up when you run a program, and then the information printing to screen sort of like how you can input information inside of Command Prompt without having to put the information inside a secondary window.

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

enter image description here This is what im trying to avoid The program makes the user input data on the right screen rather than having them input the data within the actual window itself. How would I go about achieving this?

2 Answers

Although not typing on the turtle window itself, rather than the console, user input can be obtained via pop-up windows using two input methods turtle provides:

textinput(title, prompt)

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

These are designed to prevent some errors that console style input needs to trap. They were introduced in Python 3 and are not available in Python 2.

If you do a listen() in your code to enable keyboard events, you'll need to redo the listen() command after invoking the above as they become the listener.

Related