Bind only fires off once? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: Bind only fires off once? (/thread-14815.html) |
Bind only fires off once? - WuchaDoin - Dec-18-2018 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. RE: Bind only fires off once? - metulburr - Dec-18-2018 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() RE: Bind only fires off once? - WuchaDoin - Dec-18-2018 Oops, yes. "ComboboxSelected". I forgot to change that back when I was trying to find a solution. Mother.. 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") RE: Bind only fires off once? - buran - Dec-18-2018 (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
|