Bottom Page

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.
Quote
#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.
Quote
#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!
Quote
#4
usergbp.bind('<Return>', on_entry_function)
Quote
#5
(Aug-29-2019, 08:40 PM)Yoriz Wrote:
usergbp.bind('<Return>', on_entry_function)
Thank you a lot!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [WxPython] Bind error PeterLinux 1 95 8 hours ago
Last Post: joe_momma
  Transfer Toplevel window entry to root window entry with TKinter HBH 0 920 Jan-23-2020, 09:00 PM
Last Post: HBH
  GUI freezes while executing a callback funtion when a button is pressed abi17124 5 1,156 Jul-10-2019, 12:48 AM
Last Post: FullOfHelp
  [Tkinter] how to get the entry information using Entry.get() ? SamyPyth 2 857 Mar-18-2019, 05:36 PM
Last Post: woooee
  Update plot by <Return> bind with entry widget Zorro 1 810 Mar-09-2019, 12:27 PM
Last Post: Zorro
  [Kivy] Chagne a button's function after its first pressed TheStraying11 2 924 Feb-17-2019, 06:16 PM
Last Post: Yoriz
  Bind only fires off once? WuchaDoin 3 810 Dec-18-2018, 07:46 PM
Last Post: buran
  [WxPython] bind label and entry text with return key metulburr 1 977 Aug-14-2018, 10:02 PM
Last Post: metulburr
  [Tkinter] Is there any way to bind keys to a gui window in tkinter? Nwb 1 982 Jun-21-2018, 06:04 PM
Last Post: Larz60+
  how to bind entry to a function in another file Rishav 1 1,157 Aug-07-2017, 04:34 PM
Last Post: metulburr

Forum Jump:


Users browsing this thread: 1 Guest(s)