I am working on a Qt/Python program, which includes one QMainWindow, that also has two widgets (Let's call them Widget A and Widget B).
I would like to get some value by running "someFunction()" which is arranged in Widget A from Widget B. So, I tried the following code in the constructor of Widget B:
self.someValue = self.parent().findChild(WidgetB).someFunction()
But it do not work at all. If I run this function in the constructor I do not even get any error or something.
Could someone explain this behaviour? Or better, tell me how to run this function in another widget!?
Thanks in advance!
I found the solution!
The problem was, that the parent-widget in the constructor has another parent than the rest of the class... or something like this.. but anyway, here is the code that worked properly:
self.parent().parent().findChild(widgets.WidgetA).someFunction()
Hope that I can help anyone who also has this problem!
Related
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.
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!
I am tryin to create a simple calculator in PyQt, I am a newbie to PyQt. I have used QGridLayout to display the buttons and now I want to create the signals and the slots such that when the user clicks button2 ,2 displays in the QLineEdit. I have been trying to identify the individual buttons in the grid so I can create the slots for it but I am stuck. Here is a code snippet:
for num in range(1,9):
grid.itemAt(num)=widget
widget.clicked.connect(Display_0)
the error is can't assign to function call. Please help I have researched and researched about it and once when I was experimenting I got an error, QWidgetItem does not have the attribute clicked.Thanks
I'll try to help here.
In your code, grid.itemAt(num) is the execution of the function grid.itemAt. Assigning the widget variable to the execution of a function doesn't make much sense. Maybe you meant this instead?
widget=grid.itemAt(num)
This assigns the output of grid.itemAt(num) to the widget variable.
Hope this helps!
I probably should post this in multiple questions.
I need to know two things, I have a simple window that all it has is a button within it, set like so:
self.spherize = QtGui.QPushButton("Spherize")
I'm just wondering how to set the height of this button?
I've tried the following but it didn't work:
self.spherize.setHeight(50)
Also, where can I find propper docs for things like this with example code?
This is PyQT documentation for QPushButton, but it inherits from QAbstractButton and QWidget which has the methods for changing widget size.
The call you want is QWidget.setFixedHeight. So you want something like:
self.spherize.setFixedHeight(50)
Here is the PySide documentation.
As far as I know, only gtk.CellRendererToggle and gtk.CellRendererText can be activated. I have a gtk.CellRendererPixbuf in a gtk.TreeView which I want to make it emit a signal when clicked.
I read that the activate() function can do the job but I do not know how to implement it.
def activate(event, widget, path, background_area, cell_area, flags)
What does each of these arguments mean and how do I set/obtain them? Any examples would be extremely helpful.
Thanks in advance
I have not tried this myself before, but your question has been asked before, maybe this is of some help for you:
gtk treeview: place image buttons on rows