Tkinter: Can you see all bindings of a widget? - python

In my application I want to toggle between two states, and in each of the states I want -> don't want various keys bound.
Right now what I've done is that in my class I've created an attribute self.bindings = [] and then I have a method to create bindings:
def _create_bindings(self):
self.bind("<Button-1>", self._canvas_on_click)
self.bindings.append("<Button-1>")
self.bind("<Double-Button-1>", self._canvas_on_2click)
self.bindings.append("<Double-Button-1>")
self.bind("<<arrow>>", self._canvas_on_arrows)
self.bindings.append("<<arrow>>")
self.bind("<space>", lambda event: self._toggle_selected())
self.bindings.append("<space>")
self.bind("<Key>", self._canvas_on_key_press)
self.bindings.append("<Key>")
self.bind("<BackSpace>", lambda event: self._empty_cell())
self.bindings.append("<BackSpace>")
self.bind("<Escape>", self._esc)
self.bindings.append("<Escape>")
and then to remove them:
def _remove_bindings(self):
for b in self.bindings:
self.unbind(b)
It's not terrible but it does lead to some duplication (see create function: create binding + add to list).
I could create a wrapper to combine these two steps, but regardless I still have an extra attribute to manage.
Is there a function which I can call which would provide me the same information as self.bindings above?

If you call the bind method without any parameters it will return a list of all events that you have bound for that widget. Your _remove_bindings could look like this:
def _remove_bindings(self):
for event in self.bind():
self.unbind(event)

There is no need to remove the bindings.
Altering the 'state' attribute of the e.g. Canvas, Button disables any bindings.

Related

Expand Python Tkinter Object Types

There is this question to discover existing types:
Getting Python Tkinter Object Type
However I've just developed tooltips (balloons) I've assigned to some buttons and would like to be able to recall all of them as a unique type. Also down the road I'd like to hand-craft canvas objects which will operate like pseudo buttons with <enter>, <leave>, <active>, <press> and <release> events. How might I declare a new object type for them?
If I understand your question correctly you want to check if an instance created is a instance of the custom class, it can be directly done using isinstance:
class CustomTooltip():
pass
cwidget = CustomTooltip()
btn = tk.Button(root)
print(isinstance(cwidget, CustomTooltip)) # True
print(isinstance(b, CustomTooltip)) # False
print(type(cwidget) == CustomTooltip) # Not recommended
It is recommended to use isinstance rather than type, from the docs:
The isinstance() built-in function is recommended for testing the type of an object, because it takes subclasses into account.
I did not quite get your 2nd question, also it is better to keep a single question focused around one main question rather than asking more than one question.
Object Oriented Programming is probably the solution.
If I want to create a "new type" of tkinter button I can sub-class it.
class MySpecialButton(tkinter.Button):
pass
This doesn't do anything special at the moment but will give it a unique type (If it have understood your interpretation correctly)
The following example from another one of my answers, creates a special button with custom behaviour for hovering over the button
class HoverButton(tk.Button):
def __init__(self, master, **kw):
tk.Button.__init__(self,master=master,**kw)
self.defaultBackground = self["background"]
self.bind("<Enter>", self.on_enter)
self.bind("<Leave>", self.on_leave)
def on_enter(self, e):
self['background'] = self['activebackground']
def on_leave(self, e):
self['background'] = self.defaultBackground
With regard to canvas object, You can obviously create classes for these too which can contain methods for moving/resizing the object. As to how to create custom events for these, you can use tag_bind(item, event=None, callback=None, add=None) to bind a call back to a canvas object. A quick example below
import tkinter as tk
class CanvasShape:
def __init__(self, canvas, callback = None):
self.canvas = canvas
self.id = canvas.create_oval(10,10,50,50,fill='blue')
self.canvas.tag_bind(self.id,'<Button-1>',callback)
def clicked(e):
print("You clicked on a shape")
root = tk.Tk()
c = tk.Canvas(root,width=200,height=200)
c.grid()
shape = CanvasShape(c,callback=clicked)
root.mainloop()
This will create a circle that when you click on it will fire an event that is received by the clicked function.

Getting tkinter.Widget bindings

Is there a way to get the default binding of tk.Widget class and then call it?
I need to do this because I need to call the default binding before my custom binding. It is called as the last by default.
So what I want to do is: bind my widget to my own callback, get the default binding function, call the default binding function, call my fuction (custom binding)
If you want to simply reverse the order that the bindings are processed, you can do that by changing the binding tags without needing to know what the actual bindings are. The tags are what determines the order that events are processed. By default the value is the widget, the widget class, the toplevel window, and then "all".
The following example changes the order so that the class binding ("Entry") is handled before the binding of the widget:
import tkinter as tk
...
e = tk.Entry(...)
e.bindtags((
e.winfo_class(),
str(e),
e.winfo_toplevel(),
"all")
)

Re-binding “select all” in Entry widget

My question is related to this one where a Text widget is used.
However, in my case I want to rebind the select all on the entry widget.
I tried the following which allows me to use Ctrl+w to select all input in the entry field:
self.frmSearch = Frame()
self.txtSearch = Entry(self.frmSearch, bd=1, width=35)
self.txtSearch.bind('<Control-w>',lambda e: self.txtSearch.select_range(0, END))
However, once I change Ctrl+w to Ctrl+a this does not work anymore and no text is selected. Does anyone have an explanation why?
It is because you are putting the binding on the widget rather than the widget class, and by default the bindings on the class fire after the bindings on the widget.
The way Tkinter processes events is to first see if there is a binding on a widget, then on a class, and then on the toplevel window, and then finally on the special class "all". The events are processed in order unless you break the chain of events, so to speak. So, your control-w binding happens, but then the binding on the class happens and effectively undoes what you you did in your binding.
The best solution is to 1) not use lambda, but instead use a real method or function, and 2) do a "return 'break'" which prevents the class and other bindings from firing. Or, if you want this binding to affect all entry widgets in your application rather than just a specific one, use bind_class giving the class name of 'Entry'.
The question you refer to you in your question has an answer that gives an example of changing the class binding.

Get the object that invoke an event

If I have two objects that will call the same methode then an event happens is it possible to see which of them that invoked the event?
To make it cleaer. If I have two buttons and one methode that are called then I click on them. What can I do in this methode to see whitch button that was clicked?
...
buttonA.Bind(wx.EVT_BUTTON ,self.methode)
buttonB.Bind(wx.EVT_BUTTON ,self.methode)
...
...
def methode(self,event)
#get the button that was clicked
Try this:
...
buttonA.Bind(wx.EVT_BUTTON ,self.methode)
buttonB.Bind(wx.EVT_BUTTON ,self.methode)
...
...
def methode(self, event)
#get the button that was clicked
button = event.GetEventObject()
print button.GetLabel()
The simplest approach would be to create two separate methods:
buttonA.Bind(wx.EVT_BUTTON, self.method_from_A)
buttonB.Bind(wx.EVT_BUTTON, self.method_from_B)
If these two methods share code, then they could both call some other helper method.
Instead of naming them something arbitrary like method_from_X, try to pick names that would clarify why the cases are different. For names, focus on the "why" rather than on implementation details.
If you really want to have a single callback method, you can follow the instructions here on Passing Arguments to Callbacks:
http://wiki.wxpython.org/Passing%20Arguments%20to%20Callbacks

how to update a wxlistbox from a different class?

i have a wxlistbox in a class and i want to update the data inside the listbox from a different class.Is i possible to reload the class while leave the control from another class?if yes ,how?
eg:
i have two classes,Class A and Class B.In class A there is a wxlistbox.while starting the program class A initilise the wxlistbox and bind some values.when a button inside class A clicked it call another frame class B.while close the frame B the wxlistbox inside class A should update.
My question is how to refresh listbox while close the frame B?
I would use the SetItems() method, which according to the docs does the following: "Clear and set the strings in the control from a list".
Edit: myListCtrl.SetItems(ListOfStrings)
That will replace all the items in the control with whatever is in the list.

Categories

Resources