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:

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.
Reply
#2
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()
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
lambda x:
solution:

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
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 lambda keypress counter knoxvilles_joker 15 1,685 Apr-19-2021, 01:56 AM
Last Post: knoxvilles_joker
  [Tkinter] Mouse click without use bind ATARI_LIVE 8 2,472 Oct-23-2020, 10:41 PM
Last Post: ATARI_LIVE
  [Kivy] AttributeError: 'NoneType' object has no attribute 'bind' faszination_92 2 3,010 Apr-12-2020, 07:01 PM
Last Post: Larz60+
  [WxPython] Bind error PeterLinux 1 973 Apr-06-2020, 03:07 AM
Last Post: joe_momma
  Tkinter:Unable to bind and unbind function with a button shallanq 2 2,108 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 1,910 Aug-29-2019, 10:11 PM
Last Post: Michael4
  Update plot by <Return> bind with entry widget Zorro 1 2,181 Mar-09-2019, 12:27 PM
Last Post: Zorro
  [WxPython] bind label and entry text with return key metulburr 1 1,731 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 1,754 Jun-21-2018, 06:04 PM
Last Post: Larz60+
  how to bind entry to a function in another file Rishav 1 1,828 Aug-07-2017, 04:34 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