Python Forum

Full Version: Bind only fires off once?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a combobox used with Tkinter I cannot get bind to execute appropriately. Currently I have:

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

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.
Its been awhile since i did tkinter so excuse me for being a little off.

Do you mean ComboboxSelected?

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()
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()
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
lambda x:
solution:

self.ap_reason.bind("<<ComboboxSelected>>", lambda x: Callback())

def Callback():
     print("here")
(Dec-18-2018, 07:34 PM)WuchaDoin Wrote: [ -> ]self.ap_reason.bind("<<ComboboxSelected>>", lambda x: Callback())
I think you want
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