Python Tkinter name input and message display
Write a Python program that allows users to input their name into a Tkinter Entry widget. When they press Enter or click a button, display a message with their name.
Sample Solution:
Python Code:
Explanation:
In the exercise above -
- Import the "tkinter" library.
- Define the “display_name()” function, which retrieves the text entered in the 'Entry' widget, and if a name is provided, it updates the 'message_label' with a message.
- Create the main window and set its title.
- Create a 'Label' widget (instruction_label) to provide instructions to the user.
- Create an 'Entry' widget (name_entry) where the user can input their name.
- Create a button (submit_button) with the label 'Submit' that, when clicked, calls the "display_name()" function.
- Create a Label widget (message_label) that initially displays an empty message but updates with the user's name when submitted.
- We bind the 'Enter' key to call the "display_name()" function, allowing the user to press Enter after entering their name.
- Finally, start the Tkinter main loop with "root.mainloop()".
Output:
Flowchart:

Go to:
Previous: Python Tkinter Color Picker Example.
Next: Browse and display folder contents.
Python Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.