Identify items in a QGridLayout in PyQt4 - python

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!

Related

Step-by-step tkinter GUI layout

How do I make a step-by-step GUI Layout with Tkinter Python 3.7? What I mean is that I want to have the user enter some information, press the "NEXT" button and enter some more information, etc. I don't think there's really a feasible way to completely change the layout like this with Tkinter, so I'm hoping there's something I'm missing. How do I do this?
I don't think there's really a feasible way to completely change the layout like this with Tkinter,
That is incorrect. This is trivially easy with Tkinter. Create a function or class for each step. All of the widgets for that step should be inside a single frame.
You then just need to call the first function or class to create the frame. When the user clicks "next", destroy the frame and create the next frame. And so on.

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.

Background process and tkinter

Looking for help on where to start with this, not too good with Python. What I trying to do is use tkinter for a gui interface but i need to be able to process recieved data and update labels widgets as information changes. I all ready have the communication portion of my program working fine in the shell but when I try to tie it to tkinter it will stop processing as soon as the interface is generated. Anyone have a simple code for me to modify to my needs or point me to a reference example somewhere. Spent days so far trying different options and I still have yet to find something that works.
Thanks for any help
Convert your working program into functions that you can register as callbacks in the tkinter UI (say buttons, or other widgets), that is, make it event-driven, and then, for background processing register some of the functions with the after widget method. The root.mainloop() will never return (only on UI close), use it as the last instruction.
So you can't just write your logic in a top-down structure, and hope that it will work well with the UI. The mainloop will be permanently looping, and will call specific funtions in your code, as appropriate to the received events from the user, or to callbacks you registered to run after some time with after.
See here for the after part
Take a look here for structuring tkinter programs. It should have enough info and links for you to study and learn how to do it in a right way.

How to use pyqt widget event() method?

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!

How to get access to another qt widget function?

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!

Categories

Resources