Python Forum
[Tkinter] How to bind an event when enter is pressed on a Entry control?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] How to bind an event when enter is pressed on a Entry control?
#1
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.
Reply
#2
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.
Reply
#3
(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!
Reply
#4
usergbp.bind('<Return>', on_entry_function)
Reply
#5
(Aug-29-2019, 08:40 PM)Yoriz Wrote:
usergbp.bind('<Return>', on_entry_function)
Thank you a lot!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] bind lambda keypress counter knoxvilles_joker 15 1,401 Apr-19-2021, 01:56 AM
Last Post: knoxvilles_joker
  TKinter restarting the mainloop when button pressed zazas321 7 2,100 Jan-26-2021, 06:38 AM
Last Post: zazas321
  [Tkinter] Mouse click without use bind ATARI_LIVE 8 2,193 Oct-23-2020, 10:41 PM
Last Post: ATARI_LIVE
  [Kivy] AttributeError: 'NoneType' object has no attribute 'bind' faszination_92 2 2,750 Apr-12-2020, 07:01 PM
Last Post: Larz60+
  [WxPython] Bind error PeterLinux 1 916 Apr-06-2020, 03:07 AM
Last Post: joe_momma
  Tkinter:Unable to bind and unbind function with a button shallanq 2 1,831 Mar-28-2020, 02:05 AM
Last Post: joe_momma
  Transfer Toplevel window entry to root window entry with TKinter HBH 0 2,146 Jan-23-2020, 09:00 PM
Last Post: HBH
  GUI freezes while executing a callback funtion when a button is pressed abi17124 5 3,304 Jul-10-2019, 12:48 AM
Last Post: FullOfHelp
  [Tkinter] how to get the entry information using Entry.get() ? SamyPyth 2 1,779 Mar-18-2019, 05:36 PM
Last Post: woooee
  Update plot by <Return> bind with entry widget Zorro 1 2,051 Mar-09-2019, 12:27 PM
Last Post: Zorro

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020