Python Forum
Bind only fires off once?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Bind only fires off once?
#1
I have a combobox used with Tkinter I cannot get bind to execute appropriately. Currently I have:

1
self.ap_reason.bind("<<ComboboxChanged>>", self.Callback())
This executes the following (or should, rather):

1
2
def Callback(self):
     print("here")
If I use lambda, it doesn't execute at all. If I remove lambda, then the bind executes as soon as the toplevel opens and not when changed. I've run this through debug so I can see where it's not executing the function properly - or at all. Not sure what else I should post here 'code-wise' so please feel free to ask if more information is neededed.
Reply
#2
Its been awhile since i did tkinter so excuse me for being a little off.

Do you mean ComboboxSelected?

1
2
3
4
5
6
7
8
9
10
11
import tkinter as tk
from tkinter import ttk
 
tkwindow = tk.Tk()
 
cbox = ttk.Combobox(tkwindow, values=[1,2,3], state='readonly')
cbox.grid(column=0, row=0)
 
cbox.bind("<<ComboboxSelected>>", lambda event: print(f"Selected! {event}"))
 
tkwindow.mainloop()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import tkinter as tk
from tkinter import ttk
 
tkwindow = tk.Tk()
 
cbox = ttk.Combobox(tkwindow, values=[1,2,3], state='readonly')
cbox.grid(column=0, row=0)
 
 
def callback(event):
    print(f'changed: {event}')
 
cbox.bind("<<ComboboxSelected>>", callback)
 
tkwindow.mainloop()
Recommended Tutorials:
Reply
#3
Shocked Oops, yes. "ComboboxSelected". I forgot to change that back when I was trying to find a solution.

Mother.. Wall I always - Always - find the solution as soon as I posted a question.
So I had to move my function down one hierarchy and add
1
lambda x:
solution:

1
2
3
4
self.ap_reason.bind("<<ComboboxSelected>>", lambda x: Callback())
 
def Callback():
     print("here")
Reply
#4
(Dec-18-2018, 07:34 PM)WuchaDoin Wrote: self.ap_reason.bind("<<ComboboxSelected>>", lambda x: Callback())
I think you want
1
self.ap_reason.bind("<<ComboboxSelected>>", Callback)
what you do is - call Callback, it will print (at the time when you bind) then return None and what you bind is lambda x: None
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] bind menator01 1 1,949 Apr-15-2022, 08:47 PM
Last Post: menator01
  How to move in entries using the arrow keys by applying the bind and focus? pymn 4 9,678 Apr-06-2022, 04:29 AM
Last Post: pymn
  [Tkinter] bind lambda keypress counter knoxvilles_joker 15 11,410 Apr-19-2021, 01:56 AM
Last Post: knoxvilles_joker
  [Tkinter] Mouse click without use bind ATARI_LIVE 8 12,131 Oct-23-2020, 10:41 PM
Last Post: ATARI_LIVE
  [Kivy] AttributeError: 'NoneType' object has no attribute 'bind' faszination_92 2 7,683 Apr-12-2020, 07:01 PM
Last Post: Larz60+
  [WxPython] Bind error PeterLinux 1 3,074 Apr-06-2020, 03:07 AM
Last Post: joe_momma
  Tkinter:Unable to bind and unbind function with a button shallanq 2 6,200 Mar-28-2020, 02:05 AM
Last Post: joe_momma
  [Tkinter] How to bind an event when enter is pressed on a Entry control? Michael4 4 5,591 Aug-29-2019, 10:11 PM
Last Post: Michael4
  Update plot by <Return> bind with entry widget Zorro 1 5,074 Mar-09-2019, 12:27 PM
Last Post: Zorro
  [WxPython] bind label and entry text with return key metulburr 1 4,144 Aug-14-2018, 10:02 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

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