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
Related
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.
I am learning Gtk programming with python from this. But I can not understand what is the reason for the widget keyword in the following callback definition? Can someone please explain what it does?
def on_button_clicked(self, widget):
print("Hello World")
When the button is clicked, the on_button_clicked method is executed. When this happens, the window and button objects are passed to the method as self and widget, respectively. This is useful in cases where one needs to do something with the button, such as getting its state or changing its text.
Without the widget parameter, the button object would be passed to the method and there wouldn't be a parameter to receive it, and you would get a TypeError.
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.
There's a combobox event in my code:
self.combobox1.Bind(wx.EVT_COMBOBOX, self.onActionCombobox1)
It executes the function
def onActionCombobox1(self, event):
Is there a way to execute this function automatically everytime when I start my program?
I tried it like this:
self.onActionCombobox1(event)
but it says Undefined variable: event
if you do not make use of the event variable you want to do:
self.onActionCombobox1(None)
This, set in the __init__ method of your class, will execute the method at class instantiation (not necessarily equivalent to program startup as in wxPython you can have windows/widgets that can be created dynamically at run time. You could call the method from the class before instantiating it but then method actions have not to be related with any widget state or behavior as they do not exist yet. Anyway, in that case maybe the method should be written better as an independent function).
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.