Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating Text In Entry (Tkinter)

The piece of code below takes input from user through a form and then returns the input as multiplied by 2. What I want to do is, when a user types a number (for example 5) and presses the "Enter" key on keyboard or clicks on "Calculate" button, the place where he entered the number "5" should also display 10, besides the place immediately below. Normally, the form keeps the number entered , but the place right below it gets updated and displays 10 (let us say you have entered 5)

How can I also update the form place?

(Please let me know if my question is unclear, so I can better explain myself.)

from tkinter import *

def multiplier(*args):
    try:
        value = float(ment.get())
        result.set(value * 2)
    except ValueError:
        pass

mGui = Tk()
mGui.geometry("300x300+300+300")

ment = StringVar()
result = StringVar()

mbutton = Button (mGui, text = "Calculate", command = multiplier)
mbutton.pack()

mEntry = Entry(mGui, textvariable = ment, text="bebe")
mEntry.pack()

mresult = Label(mGui, textvariable = result)
mresult.pack()
like image 201
Friendly User Avatar asked May 24 '14 17:05

Friendly User


People also ask

What does update () do in Tkinter?

Python Tkinter Mainloop Update Update() method in mainloop in Python Tkinter is used to show the updated screen. It reflects the changes when an event occurs.

How do I change StringVar value?

You need to set a textvariable , not text . This will allow the Label contents to match whatever is currently in the StringVar .

What is the difference between text and entry in Tkinter?

To enter multiple lines of text, use the Text widget. Entry in the Tkinter reference starts with: The purpose of an Entry widget is to let the user see and modify a single line of text. If you want to display multiple lines of text that can be edited, see Section 24, “The Text widget”.

How do you add text to Tkinter?

In order to add text inside a tkinter frame, we can use the create_text() method. We can define create_text() by adding values of font, text, and other options such as create_text(x,y,font, text, options….).

How to put text in the entry box in Python Tkinter?

Python Tkinter entry set text Set text is used to set the text in the entry box in Python tkinter. It is used with the function & plays the role of putting text in the entry box. we will create a function & will call the function using button.

How to update the text of a label In Tkinter?

Instead of using Text, you should use Label. With a function called StringVaryou can update the label's text. A label is a widget which can display text onto the tk window. To use the Labelcommand and StringVar, you need to:

How to set default text In Tkinter entry widget?

The Tkinter Entry widget does not have any text attribute that can be used to set the default text. Therefore, default text must be added to any text field in Tkinter using the following methods: The tkinter module is imported. The root widget is created and this must be done before creating any other widget.

What is Tkinter typebox?

It is a one-liner box wherein the user can type something. Unlike java, Tkinter does not have separate multiple line entry widgets. To allow multiple line input, increase the height of the entry widget. You may like, Python Tkinter Title.


2 Answers

You can use Entry's delete and insert methods.

from tkinter import *

def multiplier(*args):
    try:
        value = float(ment.get())
        res = value *2
        result.set(res)
        mEntry.delete(0, END) #deletes the current value
        mEntry.insert(0, res) #inserts new value assigned by 2nd parameter

    except ValueError:
        pass

mGui = Tk()
mGui.geometry("300x300+300+300")

ment = StringVar()
result = StringVar()

mbutton = Button (mGui, text = "Calculate", command = multiplier)
mbutton.pack()

mEntry = Entry(mGui, textvariable = ment, text="bebe")
mEntry.pack()

mresult = Label(mGui, textvariable = result)
mresult.pack()
like image 103
Lafexlos Avatar answered Sep 19 '22 01:09

Lafexlos


The StringVars you update via the set method, which you're doing in the multiplier function. So you question is how to trigger the call the multiplier when the user presses enter, you can use:

    mGui.bind('<Return>', multiplier)

Do you also want to change the text in the Entry? The question is a bit unclear. You can do that via ment.set as well.

like image 42
Alex Z Avatar answered Sep 22 '22 01:09

Alex Z