-1

so i wanna bind an enter button to the function so it will work when i press the button with enter button. but, everytime i do that the button always has a weird bug sometime it always getting in a pressed state or sometime the button act as i write an foregroundonactive parameter in the code.

i always write like this

def itung():
   import speedtest
   st = speedtest.Speedtest()
   lbl['text']=round(st.download/1000000,2)

lbl= Label(root,width=5,height=3)
lbl.place(x=n,y=n)#n here is just an example
btn = Button(root, text='tombol', bg='brown', fg='yellow')
btn.place(x=n,y=n)#n here is just an example
btn.bind('<Return>',itung)

so can someone help me?

  • Please provide a [mcve] that can reliably reproduce the problem. – Bryan Oakley Aug 15 '20 at 00:39
  • Your code should raise exception because `itung()` function should expect an argument when it is triggered by enter key pressed. – acw1668 Aug 15 '20 at 00:58
  • Do you think this will answer your question? https://stackoverflow.com/questions/13326940/python-tkinter-how-to-bind-key-to-a-button – Joe Ferndz Aug 15 '20 at 04:06
  • thanks for all your answer. but, that's not my problem. it just that i forget to write it like this itung(event) in stackoverflow. because, im in hurry that day so i can't double-check it that time. but, thanks for the respond – Dsterror Aug 17 '20 at 12:31

2 Answers2

0

You should try this def itung(event = None): in your function. This ensures that the itung receives a None argument as expected, when the key is pressed.

astqx
  • 2,058
  • 1
  • 10
  • 21
  • thanks for all your answer. but, that's not my problem. it just that i forget to write it like this itung(event) in stackoverflow. because, im in hurry that day so i can't double-check it that time. but, thanks for the respond – Dsterror Aug 17 '20 at 12:32
0

Hi @Dsterror and welcome to the Stack Overflow community! I've seen your question and it seems you just need a little tweak in your code:

#add *args in your parameters for the itung function
def itung(*args):
   import speedtest
   st = speedtest.Speedtest()
   lbl['text']=round(st.download/1000000,2)

lbl= Label(root,width=5,height=3)
lbl.place(x=n,y=n)#n here is just an example
btn = Button(root, text='tombol', bg='brown', fg='yellow')
btn.place(x=n,y=n)#n here is just an example
btn.bind('<Return>',itung)

You can replace the *args with any other parameter variable

Now some explanation.

Whenever you bind a key or a mouse button to a widget, tkinter expects you to create a parameter in the function which gets triggered. You may ask why is this though? What's the relevance? Imagine a left mouse button keybind to the Entry widget. Whenever you click on it, a function should get triggered. Sometimes, we may need the x and y coordinates of the cursor at the time of clicking on the Entry. So tkinter stores this in a list and gives it to the function you're binding to as a parameter. This is why we put in the *args.

Hope you understood something. If not, please tell me the unclear points

Pranav Krishna S
  • 324
  • 2
  • 13
  • i already know about that. and i only write it like that in here because i'm in hurry. and i didn't check it twice. my code is running as like i want. the no error or anything. but, i find a bug where everytime i run my app, and when the exception is thrown (i write a connection to my database if there is no username and pass match in my db the code will thrown an messagebox that showing an error message), the button in my app visually always on an active state but the button is still working normally. – Dsterror Aug 17 '20 at 12:29