Python 3, Tkinter 8.6. GUI examples in Windows 10
Probably one of the most common things to do when using a Graphical User Interface (GUI) is to display something from an entry from the user.
Below is a simple example that allows the user to input text in a Tkinter Entry field and when they click “Enter” or use the <Return> or <Enter> button it will be displayed in a Tkinter Label.
The end result will look a little like this:
The Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# Python 3, Tkinter 8.6 # Display Entry in a Label from tkinter import * root = Tk() def returnEntry(arg=None): """Gets the result from Entry and return it to the Label""" result = myEntry.get() resultLabel.config(text=result) myEntry.delete(0,END) # Create the Entry widget myEntry = Entry(root, width=20) myEntry.focus() myEntry.bind("<Return>",returnEntry) myEntry.pack() # Create the Enter button enterEntry = Button(root, text= "Enter", command=returnEntry) enterEntry.pack(fill=X) # Create and emplty Label to put the result in resultLabel = Label(root, text = "") resultLabel.pack(fill=X) root.geometry("+750+400") root.mainloop() |
The Breakdown
Getting Tkinter Ready
Line 4 to 6 and 30 to 32 sets up the window in a look. Geometry on line 30 is called to position it a little closer to center.
Creating the Widgets
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# Create the Entry widget myEntry = Entry(root, width=20) myEntry.focus() myEntry.bind("<Return>",returnEntry) myEntry.pack() # Create the Enter button enterEntry = Button(root, text= "Enter", command=returnEntry) enterEntry.pack(fill=X) # Create and emplty Label to put the result in resultLabel = Label(root, text = "") resultLabel.pack(fill=X) |
myEntry
The first widget set (line 15-19) creates the Entry bar with the variable myEntry
. Entry creates a single line widget that the user can add text to.
On the second line of myEntry
the focus is set so that when the program is first run, the user can just start typing in the entry field.
This variable is then bound to the <Return>
key – also known as the <Enter>
key. When <Enter>
is pressed, it will run the returnEntry
function.
Note: When using key binding, the action will return a value with a heap of data in it like follows:
<KeyPress event state=Mod1 keysym=Return keycode=13 char='\r' x=69 y=24>
We won’t be using this, but it does mean we will need to create an argument in our returnEntry
function or we will get an error.
Finally, we pack the label to display it. We’ll be stacking each widget on top of each other, so a simple pack is fine here.
enterEntry
From line 22-23 we create a Button that, when pressed, will also run the returnEntry
function.
enterEntry
is then packed to fill the full width of the window.
resultLabel
Here in line 25-26 we create an empty label in preparation for when the returnEntry
function is called.
Returning the Entry
def returnEntry
8 9 10 11 12 |
def returnEntry(arg=None): """Gets the result from Entry and return it to the Label""" result = myEntry.get() resultLabel.config(text=result) myEntry.delete(0,END) |
Line 8 creates the returnEntry
function.
We have an argument set to None because using the <return> key binding will give us an argument.
Line 10 results
calls the myEntry
Entry widget and gets the text and stores it as a string.
Line 11 then updates the resultLabel
by using config
and enters the results variable.
Finally, line 12 clears the text in the Entry
.
Want to learn how to automate your daily admin at work with Python? Udemy has some great Python automation courses that will help you learn how to automate your tasks so you can focus on what really matters.
Got a more specific problem you need help with, but don’t have the time to develop the skills? Fiverr’s your best bet to find a skilled professional to solve your problem quickly and cheaply. *
*The above affiliate links have been carefully researched to get you to what you specifically need. If you decide to click on one of these links it will cost you just the same as going to the site. If you decide to sign up, I just get a little pocket money to help pay for the costs of running this website.
Is this same to Python 2?
Hi Flox,
I am not certain. I would imagine that it may be an issue with things like bracket encapsulation.