tkinter, how to get the value of an Entry widget?
You can get what's inside Entry widget using get
method like:
entry = tkinter.Entry(root)
entryString = entry.get()
Here's an example that does around what you want:
import tkinter as tk
root = tk.Tk()
margin = 0.23
entry = tk.Entry(root)
entry.pack()
def profit_calculator():
profit = margin * int(entry.get())
print(profit)
button_calc = tk.Button(root, text="Calculate", command=profit_calculator)
button_calc.pack()
root.mainloop()
You may also want to use textvariable
option and tkinter.IntVar()
class for synchronizing integer texts for multiple widgets like:
import tkinter as tk
root = tk.Tk()
margin = 0.23
projectedSales = tk.IntVar()
profit = tk.IntVar()
entry = tk.Entry(root, textvariable=projectedSales)
entry.pack()
def profit_calculator():
profit.set(margin * projectedSales.get())
labelProSales = tk.Label(root, textvariable=projectedSales)
labelProSales.pack()
labelProfit = tk.Label(root, textvariable=profit)
labelProfit.pack()
button_calc = tk.Button(root, text="Calculate", command=profit_calculator)
button_calc.pack()
root.mainloop()
Above example shows that labelProSales
and entry
have their text
values equal at all times, as both use the same variable, projectedSales
, as their textvariable
option.
Can't get contents of tkinter Entry widget in Python 3
First I replaced edtGetMe
by self.edtGetMe
and same for edtSetMe
, so these variables are accessible from all the class functions. Then I added "insert" in your line self.edtSetMe.insert("insert", self.usrText)
. You also had indentation problems.
Try this :
from tkinter import *
# Define the class that forms my window.
class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.init_window()
# Init the class.
def init_window(self):
# Place widgets on the window.
self.pack(fill=BOTH, expand=1)
# Quit button.
btnCancel = Button (self, text="Cancel", command=self.cancel_out)
btnCancel.place (x=10, y=120)
# Action button.
btnAction = Button (self, text="Set Text", command=self.action)
btnAction.place (x=100, y=120)
# The Editboxes.
self.edtGetMe = Entry (self)
self.edtSetMe = Entry (self)
self.edtGetMe.place (y=10, x=10, width=380, height=100)
self.edtSetMe.place (y=155, x=10, width=380, height=100)
def cancel_out(self):
exit()
def action (self):
# Get the entry from the GetMe box.
self.usrText = self.edtGetMe.get()
# Stuff it into the other box.
self.edtSetMe.insert("insert", self.usrText)
def main():
root = Tk()
# Size of window.
root.geometry("400x300")
# Start Window Class.
app = Window(root)
root.mainloop()
if __name__ == '__main__':
main()
How to get the input from the Tkinter Text Widget?
To get Tkinter input from the text box, you must add a few more attributes to the normal .get()
function. If we have a text box myText_Box
, then this is the method for retrieving its input.
def retrieve_input():
input = self.myText_Box.get("1.0",END)
The first part, "1.0"
means that the input should be read from line one, character zero (ie: the very first character). END
is an imported constant which is set to the string "end"
. The END
part means to read until the end of the text box is reached. The only issue with this is that it actually adds a newline to our input. So, in order to fix it we should change END
to end-1c
(Thanks Bryan Oakley) The -1c
deletes 1 character, while -2c
would mean delete two characters, and so on.
def retrieve_input():
input = self.myText_Box.get("1.0",'end-1c')
Getting all of the Entry text content from a frame
Yes. In the example below when the button is clicked every child in a parent widget is checked whether are equal or not to be an entry, then their contents are printed:
try:
import tkinter as tk
except ImportError:
import Tkinter as tk
def get_all_entry_widgets_text_content(parent_widget):
children_widgets = parent_widget.winfo_children()
for child_widget in children_widgets:
if child_widget.winfo_class() == 'Entry':
print(child_widget.get())
def main():
root = tk.Tk()
table = tk.Frame(root)
for _ in range(3):
tk.Entry(table).pack()
tk.Button(table, text="Get",
command=lambda w=table: get_all_entry_widgets_text_content(w)).pack()
table.pack()
tk.mainloop()
if __name__ == '__main__':
main()
One could have a much better get_all_entry_widgets_text_content
method based on how the entries are instantiated, however, this one should work for all direct children regardless.
How do I get the Entry's value in tkinter?
Your first problem is that the call to get
in entry = E1.get()
happens even before your program starts, so clearly entry
will point to some empty string.
Your eventual second problem is that the text would anyhow be printed only after the mainloop finishes, i.e. you close the tkinter application.
If you want to print the contents of your Entry
widget while your program is running, you need to schedule a callback. For example, you can listen to the pressing of the <Return>
key as follows
import Tkinter as tk
def on_change(e):
print e.widget.get()
root = tk.Tk()
e = tk.Entry(root)
e.pack()
# Calling on_change when you press the return key
e.bind("<Return>", on_change)
root.mainloop()
Tkinter returning .!entry instead of text contents of the Entry
That's probably because in the input
function, you are printing
print(e1)
print(e2)
which are the Entry
objects. You might have been trying to print the title
and body
, which are being extracted, but unused.
How to get text from Entry widget
Please read and follow this SO help page. Your code is missing lines needed to run and has lines that are extraneous to your question. It is also missing indentation.
Your problem is that you call userInput.get()
just once, while creating the button, before the user can enter anything. At that time, its value is indeed ''
. You must call it within the button command function, which is called each time the button is pressed.
Here is a minimal complete example that both runs and works.
import tkinter as tk
root = tk.Tk()
user_input = tk.StringVar(root)
answer = 3
def verify():
print(int(user_input.get()) == answer) # calling get() here!
entry = tk.Entry(root, textvariable=user_input)
entry.pack()
check = tk.Button(root, text='check 3', command=verify)
check.pack()
root.mainloop()
How to set the text/value/content of an `Entry` widget using a button in tkinter
You might want to use insert
method. You can find the documentation for the Tkinter Entry Widget here.
This script inserts a text into Entry
. The inserted text can be changed in command
parameter of the Button.
from tkinter import *
def set_text(text):
e.delete(0,END)
e.insert(0,text)
return
win = Tk()
e = Entry(win,width=10)
e.pack()
b1 = Button(win,text="animal",command=lambda:set_text("animal"))
b1.pack()
b2 = Button(win,text="plant",command=lambda:set_text("plant"))
b2.pack()
win.mainloop()
how can I return the user input from entry widget in tkinter
You Have to Define variable of StringVar. then pass it through Entry widget initialization and then you can get text of the widget by setting get function for StringVar variable. here is the code.
content = StringVar()
entry = Entry(parent, text=caption, textvariable=content)
# Getting Text from Entry Widget
text = content.get()
# Setting text to Entry widget
content.set(text)
you can read documentation here : https://effbot.org/tkinterbook/entry.htm
Related Topics
How to Change Dataframe Column Names in Pyspark
How to Find Where Python Is Installed on Windows
Make Requests Using Python Over Tor
Appending to an Empty Dataframe in Pandas
How to Write Inline If Statement for Print
How to Change Plot Background Color
Defining Private Module Functions in Python
I Expect 'True' But Get 'None'
Python 3.5 - "Geckodriver Executable Needs to Be in Path"
How to Make My Player Rotate Towards Mouse Position
Wrapping a C Library in Python: C, Cython or Ctypes
What's the Best Practice Using a Settings File in Python
Pyplot Common Axes Labels for Subplots
How to Get Most Informative Features for Scikit-Learn Classifiers
How to Use Python to Execute a Curl Command
Numpy: Find First Index of Value Fast