turtle.textinput() function in Python

The turtle.textinput() function pops up a dialog window for input of a string. Returns the input string, or None if cancelled.

Syntax: turtle.textinput(title, prompt)
Parameters: title – string (title of the dialog window); prompt – string (text to describe what information to input).

Let us see an example to implement the turtle.textinput() function in Python:

Demo55.py

# turtle.textinput() function in Python
# Code by Studyopedia

import turtle

window = turtle.Screen()
t = turtle.Turtle()

# Get text input from user
name = window.textinput("Name", "What is your name?")
t.write(f"Hello, {name}!", font=("Arial", 16, "normal"))

window.exitonclick()

The following is the output:

turtle.textinput() function in Python

In the above code, we followed the below steps:

  • Import module: import turtle loads the turtle graphics library.
  • Create screen: window = turtle.Screen() opens the drawing window.
  • Create turtle: t = turtle.Turtle() creates the turtle you’ll control.
  • Prompt for text: name = window.textinput(“Name”, “What is your name?”) shows a modal input box with title “Name” and the prompt. Returns the entered string or None if the user cancels.
  • Write greeting: t.write(f”Hello, {name}!”, font=(“Arial”, 16, “normal”)) draws the text at the turtle’s current position using the specified font. It doesn’t move the turtle; if name is None, it will print “Hello, None!”.
  • Close on click: window.exitonclick() waits for a mouse click, then closes the window.

If you liked the tutorial, spread the word and share the link and our website, Studyopedia, with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

turtle.setworldcoordinates() function in Python
turtle.window_height() function in Python
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment