Python Forum
[Tkinter] How to bind an event when enter is pressed on a Entry control? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/Forum-Python-Coding)
+--- Forum: GUI (https://python-forum.io/Forum-GUI)
+--- Thread: [Tkinter] How to bind an event when enter is pressed on a Entry control? (/Thread-Tkinter-How-to-bind-an-event-when-enter-is-pressed-on-a-Entry-control)



How to bind an event when enter is pressed on a Entry control? - Michael4 - Aug-29-2019

Hello everyone! I am a newbie to Python. I'd like to create a program that runs using Tkinter, but my code doesn't work correctly, unluckily. Could you please tell me where the issue is?

from tkinter import *
def conv1(self):
    gbp0=174000000
    galleons0=34000872
    sickles0=14
    knuts0=7
    galleons1=float(galleons0+sickles0/17+knuts0/29/17)
    fracture=float(gbp0/galleons1)
    convert1=Toplevel(root)
    convert1.title("Pounds Sterling (GBP) to Galleons, Sickles and Knuts Converter")
    label1_1=Label(convert1, text="Type the amount of money in GBP that you would like to convert to Galleons, Sickles and Knuts and press Enter.")
    label1_1.pack()
    label1_2=Label(convert1, text="1 Galleon = 5.12 GBP")
    label1_2.pack()
    label1_3=Label(convert1, text='GBP:')
    label1_3.pack()
    usergbpvar=DoubleVar()
    usergbp=Entry(convert1, textvariable=usergbpvar)
    usergbp.pack()
    a=float(usergbpvar.get()/fracture)
    galleons=int(a//1)
    a=a%1
    a=a*17
    sickles=int(a//1)
    a=a%1
    a=a*29
    if a%1==0.5:
        knuts=int(round(a, 0))
        knuts=knuts+1
    else:
        knuts=int(round(a, 0))
    galleons=str(galleons)
    sickles=str(sickles)
    knuts=str(knuts)
    label1_4=Label(convert1, text=galleons)
    label1_4.pack()
    label1_5=Label(convert1, text=sickles)
    label1_5.pack()
    label1_6=Label(convert1, text=knuts)
    label1_6.pack()
    convert1.mainloop()
root=Tk()
btn1=Button(root, text='GBP to Galleons, Sickles and Knuts', bg='#555', fg='#ccc', font='16')
btn1.pack()
btn1.bind('<Button-1>', conv1)
root.mainloop()
The code is supposed to calculate the number entered by the user and to show the result consisting of three numbers. However, it shows 0.


RE: Tkinter code doesn't work - Yoriz - Aug-29-2019

You should only create one mainloop.
An event needs binding to the Entry usergbp when enter is pressed, in that event it reads the value, does the calculations and updates the label values.


RE: Tkinter code doesn't work - Michael4 - Aug-29-2019

(Aug-29-2019, 07:59 PM)Yoriz Wrote: You should only create one mainloop. An event needs binding to the Entry usergbp when enter is pressed, in that event it reads the value, does the calculations and updates the label values.
Thank you very much! I've understood the mistake with two mainloops. Sorry for this silly question, but how can I bind an event to the Entry? Should it look like "label1_4.bind('<Return>', usergbp)"? Thanks again!


RE: Tkinter code doesn't work - Yoriz - Aug-29-2019

usergbp.bind('<Return>', on_entry_function)



RE: How to bind an event when enter is pressed on a Entry control? - Michael4 - Aug-29-2019

(Aug-29-2019, 08:40 PM)Yoriz Wrote:
usergbp.bind('<Return>', on_entry_function)
Thank you a lot!