Unsolved PySide2 Spacebar press
-
if key in [Qt.Key_0, Qt.Key_1]: print("smothering")
-
@SGaist but where? I would need an event for this, seeing as the keyboardpress event doesnt trigger from it
-
-
@jsulm When I
print(dir(MainWindow))
there is noQEventKey
-
@Caeden The event is right here: def keyPressEvent(self, event)
-
@jsulm Yes, but the event isn't triggered when I press the spacebar.
def keyPressEvent(self, event) -> None: """Key press""" print('hi')
this does not print 'hi' when I press the spacebar. Spacebar is the key i'm after and it does not trigger this event
-
@Caeden One moment. In which exact widget do you want to enter space? Which widget has focus when you press space? MainWindow is a container widget containing other widgets, right?
-
@jsulm I just want it to accept a spacebar press. Here is my ui code, its automatically generated so doesnt look amazingly neat
I believe that I have oneQWidget
on my main window, because on linux for some reasonbackground-color: black;
makes it appear black on Qt Designer, but shows as a white background. It worked fine on windows before, but anyway, thats irrelevant because I'm just using a widget insteadfrom PySide2.QtCore import * from PySide2.QtGui import * from PySide2.QtWidgets import * class Ui_Dialog(object): def setupUi(self, Dialog): Dialog.resize(580, 607) Dialog.setStyleSheet(u"") self.widget = QWidget(Dialog) Dialog.setWindowTitle('Calculator') self.widget.setObjectName(u"widget") self.widget.setGeometry(QRect(0, 0, 581, 611)) self.widget.setStyleSheet(u"background-color: black;") self.label = QLabel(self.widget) self.label.setObjectName(u"label") self.label.setMargin(10) self.label.setGeometry(QRect(9, 10, 561, 121)) font = QFont() font.setPointSize(20) self.label.setFont(font) self.label.setStyleSheet(u"border: 2px solid rgb(166, 166, 166);\n" "border-radius: 10px;\n" "color: white;") self.pushButton_9 = QPushButton(self.widget) self.pushButton_9.setObjectName(u"pushButton_9") self.pushButton_9.setGeometry(QRect(20, 320, 111, 51)) self.pushButton_9.setStyleSheet(u""" background-color: black; color: white; border: 2px solid rgb(166, 166, 166); border-radius: 4px; """) self.pushButton_9.setAutoDefault(False) self.pushButton_10 = QPushButton(self.widget) self.pushButton_10.setObjectName(u"pushButton_10") self.pushButton_10.setGeometry(QRect(160, 320, 111, 51)) self.pushButton_10.setStyleSheet(u""" background-color: black; color: white; border: 2px solid rgb(166, 166, 166); border-radius: 4px; """) self.pushButton_10.setAutoDefault(False) self.pushButton_11 = QPushButton(self.widget) self.pushButton_11.setObjectName(u"pushButton_11") self.pushButton_11.setGeometry(QRect(300, 320, 111, 51)) self.pushButton_11.setStyleSheet(u""" background-color: black; color: white; border: 2px solid rgb(166, 166, 166); border-radius: 4px; """) self.pushButton_11.setAutoDefault(False) self.pushButton_12 = QPushButton(self.widget) self.pushButton_12.setObjectName(u"pushButton_12") self.pushButton_12.setGeometry(QRect(300, 460, 111, 51)) self.pushButton_12.setStyleSheet(u""" background-color: black; color: white; border: 2px solid rgb(166, 166, 166); border-radius: 4px; """) self.pushButton_12.setAutoDefault(False) self.pushButton_13 = QPushButton(self.widget) self.pushButton_13.setObjectName(u"pushButton_13") self.pushButton_13.setGeometry(QRect(160, 460, 111, 51)) self.pushButton_13.setStyleSheet(u""" background-color: black; color: white; border: 2px solid rgb(166, 166, 166); border-radius: 4px; """) self.pushButton_13.setAutoDefault(False) self.pushButton_14 = QPushButton(self.widget) self.pushButton_14.setObjectName(u"pushButton_14") self.pushButton_14.setGeometry(QRect(20, 460, 111, 51)) self.pushButton_14.setStyleSheet(u""" background-color: black; color: white; border: 2px solid rgb(166, 166, 166); border-radius: 4px; """) self.pushButton_14.setAutoDefault(False) self.pushButton_15 = QPushButton(self.widget) self.pushButton_15.setObjectName(u"pushButton_15") self.pushButton_15.setGeometry(QRect(300, 390, 111, 51)) self.pushButton_15.setStyleSheet(u""" background-color: black; color: white; border: 2px solid rgb(166, 166, 166); border-radius: 4px; """) self.pushButton_15.setAutoDefault(False) self.pushButton_16 = QPushButton(self.widget) self.pushButton_16.setObjectName(u"pushButton_16") self.pushButton_16.setGeometry(QRect(160, 390, 111, 51)) self.pushButton_16.setStyleSheet(u""" background-color: black; color: white; border: 2px solid rgb(166, 166, 166); border-radius: 4px; """) self.pushButton_16.setAutoDefault(False) self.pushButton_17 = QPushButton(self.widget) self.pushButton_17.setObjectName(u"pushButton_17") self.pushButton_17.setGeometry(QRect(20, 390, 111, 51)) self.pushButton_17.setStyleSheet(u""" background-color: black; color: white; border: 2px solid rgb(166, 166, 166); border-radius: 4px; """) self.pushButton_17.setAutoDefault(False) self.pushButton_18 = QPushButton(self.widget) self.pushButton_18.setObjectName(u"pushButton_18") self.pushButton_18.setGeometry(QRect(300, 240, 111, 51)) self.pushButton_18.setStyleSheet(u""" background-color: black; color: white; border: 2px solid rgb(166, 166, 166); border-radius: 4px; """) self.pushButton_18.setAutoDefault(False) self.pushButton_19 = QPushButton(self.widget) self.pushButton_19.setObjectName(u"pushButton_19") self.pushButton_19.setGeometry(QRect(160, 240, 111, 51)) self.pushButton_19.setStyleSheet(u""" background-color: black; color: white; border: 2px solid rgb(166, 166, 166); border-radius: 4px; """) self.pushButton_19.setAutoDefault(False) self.pushButton_20 = QPushButton(self.widget) self.pushButton_20.setObjectName(u"pushButton_20") self.pushButton_20.setGeometry(QRect(20, 240, 111, 51)) self.pushButton_20.setStyleSheet(u""" background-color: black; color: white; border: 2px solid rgb(166, 166, 166); border-radius: 4px; """) self.pushButton_20.setAutoDefault(False) self.pushButton_21 = QPushButton(self.widget) self.pushButton_21.setObjectName(u"pushButton_21") self.pushButton_21.setGeometry(QRect(20, 540, 111, 51)) self.pushButton_21.setStyleSheet(u""" background-color: black; color: white; border: 2px solid rgb(166, 166, 166); border-radius: 4px; """) self.pushButton_21.setAutoDefault(False) self.pushButton_22 = QPushButton(self.widget) self.pushButton_22.setObjectName(u"pushButton_22") self.pushButton_22.setGeometry(QRect(160, 540, 111, 51)) self.pushButton_22.setStyleSheet(u""" background-color: black; color: white; border: 2px solid rgb(166, 166, 166); border-radius: 4px; """) self.pushButton_22.setAutoDefault(False) self.pushButton_23 = QPushButton(self.widget) self.pushButton_23.setObjectName(u"pushButton_23") self.pushButton_23.setGeometry(QRect(300, 540, 111, 51)) self.pushButton_23.setStyleSheet(u"background-color: black;\n" "color: white;\n" "border: 4px solid rgb(112, 112, 112);\n" "border-radius: 4px;") self.pushButton_23.setAutoDefault(False) self.pushButton_24 = QPushButton(self.widget) self.pushButton_24.setObjectName(u"pushButton_24") self.pushButton_24.setGeometry(QRect(450, 240, 111, 51)) self.pushButton_24.setStyleSheet(u""" background-color: black; color: white; border: 2px solid rgb(166, 166, 166); border-radius: 4px; """) self.pushButton_24.setAutoDefault(False) self.pushButton_25 = QPushButton(self.widget) self.pushButton_25.setObjectName(u"pushButton_25") self.pushButton_25.setGeometry(QRect(450, 320, 111, 51)) self.pushButton_25.setStyleSheet(u""" background-color: black; color: white; border: 2px solid rgb(166, 166, 166); border-radius: 4px; """) self.pushButton_25.setAutoDefault(False) self.pushButton_26 = QPushButton(self.widget) self.pushButton_26.setObjectName(u"pushButton_26") self.pushButton_26.setGeometry(QRect(450, 390, 111, 51)) self.pushButton_26.setStyleSheet(u""" background-color: black; color: white; border: 2px solid rgb(166, 166, 166); border-radius: 4px; """) self.pushButton_26.setAutoDefault(False) self.pushButton_27 = QPushButton(self.widget) self.pushButton_27.setObjectName(u"pushButton_27") self.pushButton_27.setGeometry(QRect(450, 460, 111, 51)) self.pushButton_27.setStyleSheet(u""" background-color: black; color: white; border: 2px solid rgb(166, 166, 166); border-radius: 4px; """) self.pushButton_27.setAutoDefault(False) self.pushButton_28 = QPushButton(self.widget) self.pushButton_28.setObjectName(u"pushButton_28") self.pushButton_28.setGeometry(QRect(450, 540, 111, 51)) self.pushButton_28.setStyleSheet(u""" background-color: black; color: white; border: 2px solid rgb(166, 166, 166); border-radius: 4px; """) self.pushButton_28.setAutoDefault(False) self.pushButton_29 = QPushButton(self.widget) self.pushButton_29.setObjectName(u"pushButton_29") self.pushButton_29.setGeometry(QRect(450, 160, 111, 51)) self.pushButton_29.setStyleSheet(u""" background-color: black; color: white; border: 2px solid rgb(166, 166, 166); border-radius: 4px; """) self.pushButton_29.setAutoDefault(False) self.pushButton_30 = QPushButton(self.widget) self.pushButton_30.setObjectName(u"pushButton_30") self.pushButton_30.setGeometry(QRect(300, 160, 111, 51)) self.pushButton_30.setStyleSheet(u""" background-color: black; color: white; border: 2px solid rgb(166, 166, 166); border-radius: 4px; """) self.pushButton_30.setAutoDefault(False) self.pushButton_31 = QPushButton(self.widget) self.pushButton_31.setObjectName(u"pushButton_31") self.pushButton_31.setGeometry(QRect(160, 160, 111, 51)) self.pushButton_31.setStyleSheet(u""" background-color: black; color: white; border: 2px solid rgb(166, 166, 166); border-radius: 4px; """) self.pushButton_31.setAutoDefault(False) self.pushButton_32 = QPushButton(self.widget) self.pushButton_32.setObjectName(u"pushButton_32") self.pushButton_32.setGeometry(QRect(20, 160, 111, 51)) self.pushButton_32.setStyleSheet(u""" background-color: black; color: white; border: 2px solid rgb(166, 166, 166); border-radius: 4px; """) self.pushButton_32.setAutoDefault(False) self.retranslateUi(Dialog) QMetaObject.connectSlotsByName(Dialog) # setupUi def retranslateUi(self, Dialog): Dialog.setWindowTitle(QCoreApplication.translate("Dialog", u"Dialog", None)) self.label.setText(QCoreApplication.translate("Dialog", u"0", None)) self.pushButton_9.setText(QCoreApplication.translate("Dialog", u"7", None)) self.pushButton_10.setText(QCoreApplication.translate("Dialog", u"8", None)) self.pushButton_11.setText(QCoreApplication.translate("Dialog", u"9", None)) self.pushButton_12.setText(QCoreApplication.translate("Dialog", u"3", None)) self.pushButton_13.setText(QCoreApplication.translate("Dialog", u"2", None)) self.pushButton_14.setText(QCoreApplication.translate("Dialog", u"1", None)) self.pushButton_15.setText(QCoreApplication.translate("Dialog", u"6", None)) self.pushButton_16.setText(QCoreApplication.translate("Dialog", u"5", None)) self.pushButton_17.setText(QCoreApplication.translate("Dialog", u"4", None)) self.pushButton_18.setText(QCoreApplication.translate("Dialog", u"\u00b2", None)) self.pushButton_19.setText(QCoreApplication.translate("Dialog", u")", None)) self.pushButton_20.setText(QCoreApplication.translate("Dialog", u"(", None)) self.pushButton_21.setText(QCoreApplication.translate("Dialog", u"0", None)) self.pushButton_22.setText(QCoreApplication.translate("Dialog", u".", None)) self.pushButton_23.setText(QCoreApplication.translate("Dialog", u"=", None)) self.pushButton_24.setText(QCoreApplication.translate("Dialog", u"\u221a", None)) self.pushButton_25.setText(QCoreApplication.translate("Dialog", u"-", None)) self.pushButton_26.setText(QCoreApplication.translate("Dialog", u"/", None)) self.pushButton_27.setText(QCoreApplication.translate("Dialog", u"*", None)) self.pushButton_28.setText(QCoreApplication.translate("Dialog", u"+", None)) self.pushButton_29.setText(QCoreApplication.translate("Dialog", u"AC", None)) self.pushButton_30.setText(QCoreApplication.translate("Dialog", u"DEL", None)) self.pushButton_31.setText(QCoreApplication.translate("Dialog", u"X", None)) self.pushButton_32.setText(QCoreApplication.translate("Dialog", u"Y", None)) # retranslateUi
-
@jsulm When I do
def event(self, _event): print('hi, {}, {}'.format(_event, self.num)) self.num += 1
and press spacebar it seems that when I press
spacebar
it counts it as<PySide2.QtCore.QEvent object at 0x7f600b1b9b40>
. How do I turn this into an event function? -
@Caeden said in PySide2 Spacebar press:
event function?
Event is not a function it is an object.
_event.key()
should do.