How to use pyqt widget event() method? - python

Exactly how do I utilize the various event methods that widgets have? Say I have a comboBox(drop down list) and I want to initiate a function every time someone changes the choice. There is the changeEvent() method in the documentation but It would be great if someone explains to me with a piece of code.

This is a pretty broad question. I recommend checking out the many tutorials on Youtube.com.
However, in your init method, put something like this:
self.ui.charge_codes_combo.currentIndexChanged.connect(self.setup_payments)
In my example, the combo box was placed on a form in Qt Designer. Self.setup_payment is a method triggered by the change in the combo box.
I hope this helps!

Related

Implement a Kivy Button mouseover event

I am making a GUI that will have two buttons. I want to perform a sort of Animation on both of those when someone hovers over them.
Now, I know that using Window.bind(mouse_pos=my_mouse_pos_func) is a workaround since no on_mouse_hover is available for button.bind() like on_press and on_release. But that isn't working for me since I cannot specify which widgets in my screen to bind my function when using Window.bind(). Doing this works in a way but only if I get the cursor in my main window.
I have searched for this a lot and in almost 90% of the results I found people recommeding Window.bind(). And the other 10% are workarounds but were really unclear to me, for e.g workarounds like "listening for mouse events" and stuff like that.
Sorry, as I am being able to provide any code as I am quite clueless as to what to show. If anyone knows any simple workarounds to this, their help will be absolutely appreciated
I use the mentioned Window.bind() in the __init__() method of my app as:
Window.bind(mouse_pos=self.on_mouse_pos)
Then in the on_mouse_pos() method, I use the collide_point() method to determine if the mouse is over any of the Buttons in question:
def on_mouse_pos(self, window, pos):
for butt in self.root.ids.butt_box.children:
if butt.collide_point(*pos):
# do something here
You just need a list of the Buttons you want to check (I use the children of a container).
I know it's an older post, but just a follow-up in case someone (like me) has the same problem and finds their way here over Google. #John Anderson's solution works, but it seems easier to me to define a class for those buttons you want to display the desired behaviour and then use Window.bind() in its __init__(), e.g.
from kivy.core.window import Window
from kivy.uix.button import Button
class MyButton(Button):
def __init__(self, **kwargs):
super().__init__(**kwargs)
Window.bind(mouse_pos=self.on_mouseover)
def on_mouseover(self, window, pos):
if self.collide_point(*pos):
# desired behaviour
That way there's no need to keep the list John mentions.

python gtk toggle combobox?

Is the equivalent of ToggleButton exist in PyGtk ?
I would like to have a callback like: True or False.
self.liste = gtk.ComboBox(self.liststore)
self.liste.connect("changed", self.result_list)
With this method, the program can't detect if the user clicks again on the same choice.
Thanks
I don't have any specific advice as I haven't used ComboBox much, but I'm pretty sure that what you want to do is possible.
The GTK tutorial says
ComboBox uses a TreeModel (usually a ListStore) to provide the list
items to display.
The basic ComboBox methods are great for simple things, but for more advanced usage you need to play with things at the TreeModel level yourself. To do that effectively, you need to know how they work; fortunately the docs in the tutorial are pretty good: TreeView widget, but also check out the info in the reference manual.
To make the equivalent of a toggle button you can use 14.4.8. Activatable Toggle Cells

How to allow only one checkbox checked at a time?

I am making a GUI for a script in Python, using PySide Qt. I have a couple of checkboxes in the main window and I was wondering if it is possible to make them dependant to each other. What I mean is that, if I check one of them, all the others should become unchecked. So only one can be checked at a time.
Is there a comfortable way to do this? Otherwise I would just write a function to uncheck the others.
use QButtonGroup to make them as a group and you might want to derive a class from this and override the basic check/uncheck depending on how you want the checkboxes to behave

How to use QSignalMapper for multiple QLineEdits?

I have around 20 QLineEdits on a particular application window.
I want the content of the QLineEdit to be cleared as soon as I click on it.
For a couple of QLIneEdits I use the below lambda function.
self.StudentName.mousePressEvent = lambda event : self.StudentName.clear()
I am not sure how to implement QSignalMapper for this purpose as I have around 20 QLineEdits.
Can someone please briefly explain QSignalMapper.
Will I have to create an array of all the QLineEdit SIGNALs and then map it to the QSignalMapper SLOT.
Please suggest.
As far as I can see, QSignalMapper is for the reverse problem, or collecting and collating signals from multiple inputs.
The simplest way to set multiple events to a simple behaviour is to do it in pure python:
for edit in LineEdits:
edit.mousePressEvent = lambda event : edit.clear()
where LineEdits is your list of widgets.

wxPython: control navigation within wx.Lisbook wx.Panels

world!
I'd like to ask you one question, a simple solution (I guess) for a nerve-wracking problem I'm encountering using a wx.Lisbook component of wxPython.
In fact, I want to switch from a wx.Panel to another fluently, withou requiring a user input. I've already tried the SetFocus(), Show()/ShowWithEffect() + Hide()/HideWithEffect() methods, without great results.
Show()/Hide() gives the better results, but the selected Thumbnail remains the previous displayed panel...
Any idea of the method or good practice to manipulate wx.Listbook?
Thanks very much by advance for your answers!
Patrice
You want to be able to switch between panels? You should check out the various "Book" controls in wxPython, such as the Toolbook, Notebook, AUINotebook, etc. I also wrote a tutorial that's just on switching panels using the menu:
http://www.blog.pythonlibrary.org/2010/06/16/wxpython-how-to-switch-between-panels/
You could use a wx.Timer to do the switching too if you didn't want any user involvement.

Categories

Resources