I can't get keyPressEvent working from __init__
-
My application is laid out so almost everything is inside init, but I can't get keyPressEvent to register from within init.
If I pull it back one tab in scope, it works, but that breaks the rest of the application.
Is there something I can do to have the keyPressEvent work from within init or do I have to re-scope most of my application?
class Widget(QWidget): def __init__(self, parent=None): super().__init__(parent) self.ui = Ui_Widget() self.ui.setupUi(self) # OPTION A: Doesn't work, but preferred def keyPressEvent(self, event): print(event.key()) # OPTION B: Works def keyPressEvent(self, event): print(event.key()) if __name__ == "__main__": app = QApplication(sys.argv) widget = Widget() widget.show() sys.exit(app.exec())
Lifetime Qt Championwrote on 1 Jun 2023, 05:35 last edited by jsulm 6 Jan 2023, 05:36@user239857 said in I can't get keyPressEvent working from __init__:
I can't get keyPressEvent to register from within init.
How should that work?!
If you define keyPressEvent inside init then it only exists inside init, so Qt will not see it and will be not able to call it.
Why do you want to define methods inside init? -
My application is laid out so almost everything is inside init, but I can't get keyPressEvent to register from within init.
If I pull it back one tab in scope, it works, but that breaks the rest of the application.
Is there something I can do to have the keyPressEvent work from within init or do I have to re-scope most of my application?
class Widget(QWidget): def __init__(self, parent=None): super().__init__(parent) self.ui = Ui_Widget() self.ui.setupUi(self) # OPTION A: Doesn't work, but preferred def keyPressEvent(self, event): print(event.key()) # OPTION B: Works def keyPressEvent(self, event): print(event.key()) if __name__ == "__main__": app = QApplication(sys.argv) widget = Widget() widget.show() sys.exit(app.exec())
wrote on 1 Jun 2023, 05:46 last edited by@user239857 said in I can't get keyPressEvent working from __init__:
My application is laid out so almost everything is inside init
This makes no sense, so stop doing it.
-
@user239857 said in I can't get keyPressEvent working from __init__:
My application is laid out so almost everything is inside init
This makes no sense, so stop doing it.
wrote on 1 Jun 2023, 06:28 last edited byI'm sorry, I'm ~500 lines in having procrastinated learning how to properly scope it.
I'm not sure of the correct way to do this. I started another app on a fresh project file to experiment.
When I def functions outside of init, it can't find the ui when I try to refer to it. When I have self.ui.widgets calls outside of init, it can't find 'self' because of scope.
The last time I used Qt Creator the sample boilerplate code a new project comes with was different from what I'm accustomed to.
-
I'm sorry, I'm ~500 lines in having procrastinated learning how to properly scope it.
I'm not sure of the correct way to do this. I started another app on a fresh project file to experiment.
When I def functions outside of init, it can't find the ui when I try to refer to it. When I have self.ui.widgets calls outside of init, it can't find 'self' because of scope.
The last time I used Qt Creator the sample boilerplate code a new project comes with was different from what I'm accustomed to.
Lifetime Qt Championwrote on 1 Jun 2023, 06:30 last edited by jsulm 6 Jan 2023, 06:31@user239857 said in I can't get keyPressEvent working from __init__:
When I have self.ui.widgets calls outside
Please show the code where you're doing this. It should work just fine if the method where you're doing this also has self as first parameter.
self.ui.widgets - is there anything in your ui called "widgets"? -
I'm sorry, I'm ~500 lines in having procrastinated learning how to properly scope it.
I'm not sure of the correct way to do this. I started another app on a fresh project file to experiment.
When I def functions outside of init, it can't find the ui when I try to refer to it. When I have self.ui.widgets calls outside of init, it can't find 'self' because of scope.
The last time I used Qt Creator the sample boilerplate code a new project comes with was different from what I'm accustomed to.
wrote on 1 Jun 2023, 06:31 last edited by JonB 6 Jan 2023, 06:32@user239857 said in I can't get keyPressEvent working from __init__:
When I def functions outside of init, it can't find the ui when I try to refer to it. When I have self.ui.widgets calls outside of init, it can't find 'self' because of scope.
This is standard and basic Python. If there is no
self
it is not inside a class. Your methods need to be inside the same class as__init__()
is, but not inside__init__()
. -
wrote on 1 Jun 2023, 06:42 last edited by user239857 6 Jan 2023, 06:44
I suspect the solution isn't much more than a cut/paste away and is probably indicative of a severe lack of understanding of fundamental boilerplate stuff, which I apologize for.
class Widget(QWidget): def __init__(self, parent=None): super().__init__(parent) self.ui = Ui_Widget() self.ui.setupUi(self) def hello_world(): print("Hello world") self.ui.pushButton.pressed.connect(hello_world) if __name__ == "__main__": app = QApplication(sys.argv) widget = Widget() widget.show() sys.exit(app.exec())
Traceback (most recent call last):
File "widget.py", line 12, in <module>
class Widget(QWidget):
File "widget.py", line 22, in Widget
self.ui.pushButton.pressed.connect(hello_world)
NameError: name 'self' is not defined -
I suspect the solution isn't much more than a cut/paste away and is probably indicative of a severe lack of understanding of fundamental boilerplate stuff, which I apologize for.
class Widget(QWidget): def __init__(self, parent=None): super().__init__(parent) self.ui = Ui_Widget() self.ui.setupUi(self) def hello_world(): print("Hello world") self.ui.pushButton.pressed.connect(hello_world) if __name__ == "__main__": app = QApplication(sys.argv) widget = Widget() widget.show() sys.exit(app.exec())
Traceback (most recent call last):
File "widget.py", line 12, in <module>
class Widget(QWidget):
File "widget.py", line 22, in Widget
self.ui.pushButton.pressed.connect(hello_world)
NameError: name 'self' is not definedwrote on 1 Jun 2023, 06:45 last edited by JonB 6 Jan 2023, 06:46@user239857 said in I can't get keyPressEvent working from __init__:
self.ui.pushButton.pressed.connect(hello_world)
You cannot have this statement "free-standing" inside a class as you have done. It needs to be inside a method (
def method()
). Nor can you writehello_world
as a free-standing function reference. Please review your Python knowledge.The
connect()
statement does indeed belong inside__init__()
in this case:def __init__(self, parent=None): ... self.ui.pushButton.pressed.connect(self.hello_world)
-
@user239857 said in I can't get keyPressEvent working from __init__:
self.ui.pushButton.pressed.connect(hello_world)
You cannot have this statement "free-standing" inside a class as you have done. It needs to be inside a method (
def method()
). Nor can you writehello_world
as a free-standing function reference. Please review your Python knowledge.The
connect()
statement does indeed belong inside__init__()
in this case:def __init__(self, parent=None): ... self.ui.pushButton.pressed.connect(self.hello_world)
wrote on 1 Jun 2023, 06:54 last edited by@JonB When I have the connect() statement inside init(), it doesn't seem to recognize anything outside init though. I'm not sure what freestanding means.
-
@JonB When I have the connect() statement inside init(), it doesn't seem to recognize anything outside init though. I'm not sure what freestanding means.
wrote on 1 Jun 2023, 06:58 last edited by JonB 6 Jan 2023, 07:00@user239857 said in I can't get keyPressEvent working from __init__:
When I have the connect() statement inside init(), it doesn't seem to recognize anything outside init though
Nothing to do with being inside
__init__()
. Members of a class obviously cannot reference objects outside the class without a reference to the other objects."Freestanding" means a method or variable not inside some class.
The code I gave you will work from the code you showed. Did you try it? We don't know what else your new problem might be if you don't show an example of whatever new thing goes wrong.
-
wrote on 1 Jun 2023, 07:06 last edited by
Oh and it seems if I place (self) as first param of hello_world it's able to work outside of init. Thank you for your patience, I think this bridges the gap for me!
-
-
Oh and it seems if I place (self) as first param of hello_world it's able to work outside of init. Thank you for your patience, I think this bridges the gap for me!
wrote on 1 Jun 2023, 07:08 last edited by@user239857 said in I can't get keyPressEvent working from __init__:
Oh and it seems if I place (self) as first param of hello_world it's able to work outside of init.
Sorry, I did not notice you had
def hello_world()
instead ofdef hello_world(self)
, and I forgot my Python! Every (instance) method which is inside a class MUST haveself
as first parameter in Python (just as your__init__(self, ...)
does).
11/12