Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tkinter, how to get the value of an Entry widget? [duplicate]

I'm trying to offer the user the possibility to calculate his profit of his projected sales if the margin has a certain value (.23). The user should be able to enter any value as projected sales:

from tkinter import *

root = Tk()

margin = 0.23
projectedSales = #value of entry
profit = margin * int(projectedSales)

#My function that is linked to the event of my button
def profit_calculator(event):
    print(profit)


#the structure of the window
label_pan = Label(root, text="Projected annual sales:")
label_profit = Label(root, text="Projected profit")
label_result = Label(root, text=(profit), fg="red")

entry = Entry(root)

button_calc = Button(root, text= "Calculate", command=profit_calculator)
button_calc.bind("<Button-1>", profit_calculator)

#position of the elements on the window
label_pan.grid(row=0)
entry.grid(row=0, column=1)
button_calc.grid(row=1)              
label_profit.grid(row=2)
label_result.grid(row=2, column=1)

root.mainloop()
like image 455
Pak Avatar asked Nov 19 '17 15:11

Pak


People also ask

What is the best way to get data from an entry widget?

Let us suppose that we have created an Entry widget and we want to get the value of it. In this case, we can use the . get() method. It maps the input object into a variable which can be used further to print or display the entered value.

Which method is used to retrieve the value from an entry widget?

To return the data entered in an Entry widget, we have to use the get() method. It returns the data of the entry widget which further can be printed on the console.

What does Focus_set () do in Tkinter?

Sometimes, we need to change or modify the focus of any widget in the application which can be achieved by using the focus_set() method. This method sets the default focus for any widget and makes it active till the execution of the program.

What is IntVar () in Tkinter?

Tkinter IntVar() FunctionA variable defined using IntVar() function holds integer data where we can set integer data and can retrieve it as well using getter and setter methods.


1 Answers

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.

like image 148
Nae Avatar answered Oct 21 '22 06:10

Nae