-1

first of all I'm new to Python and coding overall. I wanna ask, I'm using IDLE and Tkinter together. I'm making a simple calculator.

def getInput():
    i1 = Input1_Box.get()
    i2 = Input2_Box.get()
    i3 = Input3_Box.get()
    calculate = float(i1) * float(i3) - float(i2) * float(i3)
    print(calculate)

Here I use define tag getInput to fetch data from Entry widget and use the string(I think so), calculate to put a simple equation to calculate all the data fetch.

But I don't know how to display the result of "calculate" into and Entry widget or display as a text.

  • Give your full code – Taohidul Islam Jun 24 '18 at 15:10
  • What exactly is not working with the given code? Shouldn't `print` do just what it should and print something? – Nico Haase Jun 24 '18 at 15:17
  • This is certainly a duplicate but I'm not sure which one it should be tagged against - see [set the text of an entry using a button tkinter](https://stackoverflow.com/questions/16373887/set-the-text-of-an-entry-using-a-button-tkinter) and [How to set specific text to Entry in Python?](https://stackoverflow.com/questions/26082226/how-to-set-specific-text-to-entry-in-python) for examples. – Alan Jun 24 '18 at 15:24

1 Answers1

0

To quote Bryan Oakley from this question and BuvinJ from this question:

For an entry widget you have two choices. One, if you have a textvariable associated, you can call set on the textvariable. This will cause any widgets associated with the textvariable to be updated. Second, without a textvariable you can use the insert and delete methods to replace what is in the widget.

Here's an example of the latter:

calculateEntry.delete(0, "end")
calculateEntry.insert(0, calculate)

And for the former:

If you use a "text variable" tk.StringVar(), you can just set() that.

No need to use the Entry delete and insert. Moreover, those functions don't work when the Entry is disabled or readonly! The text variable method, however, does work under those conditions as well.

calculateVar = tk.StringVar()
calculateEntry = tk.Entry( master, textvariable=calculateVar )
calculateVar.set( calculate )
Alan
  • 2,914
  • 2
  • 14
  • 26