Solved Class issue in connect definition
-
Can someone please help me with how I can edit
the label when the push button is clicked via the pushButton connect? Can't figure it out. The class UI_dialog is created by PYUIC in Pycharm.from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Dialog(object): def setupUi(self, Dialog): Dialog.setObjectName("Dialog") Dialog.resize(585, 321) self.label = QtWidgets.QLabel(Dialog) self.label.setGeometry(QtCore.QRect(250, 50, 61, 21)) self.label.setMinimumSize(QtCore.QSize(41, 0)) self.label.setAlignment(QtCore.Qt.AlignCenter) self.label.setWordWrap(True) self.label.setObjectName("label") self.pushButton = QtWidgets.QPushButton(Dialog) self.pushButton.setGeometry(QtCore.QRect(266, 98, 31, 23)) self.pushButton.setObjectName("pushButton") font = QtGui.QFont() font.setPointSize(12) self.retranslateUi(Dialog) self.pushButton.clicked.connect(Dialog.SB_UP_click) QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog): _translate = QtCore.QCoreApplication.translate Dialog.setWindowTitle(_translate("Dialog", "Dialog")) self.label.setText(_translate("Dialog", "SOUTH")) self.pushButton.setText(_translate("Dialog", "UP")) from PyQt5 import QtCore, QtGui, QtWidgets from My_Window import Ui_Dialog import sys class MainWindow(QtWidgets.QMainWindow, Ui_Dialog): def __init__(self): super(MainWindow, self).__init__() self.ui = Ui_Dialog() self.ui.setupUi(self) def SB_UP_click(self): print("SB UP") self.label.setText("HI") print("got here") def main(): app = QtWidgets.QApplication([]) window = MainWindow() window.show() sys.exit(app.exec_()) if __name__ == '__main__': main()
-
Late night strikes again ! :-D
I misread the code.Your slot should be something like:
def SB_UP_click(self): print("SB UP") self.ui.label.setText("HI") print("got here")
-
@heedaf said in Class issue in connect definition:
self.pushButton.clicked.connect(Dialog.SB_UP_click)
You're already connecting, so what does not work?
And please indent your code properly: it is hard to read.
-
@jsulm Sorry! On a cell phone and copied and pasted it over. When I try and write to the text of the label widget from this call I get something like label is not part of MainWindow.
-
Hi,
You do not have any widget named label_2 be it in your designer part or your MainWindow class.
You have one that is named label that you can access through "self.ui".
-
My bad! Missed that on and just fixed it.. It should be just label. I'm using PyCharm so I copied it over from their forum. I'm not at my computer but I believe I got an error with self.ui.
-
I see.
You did the connection in the wrong class.
Setup the connection in your MainWindow class not the one that is building the UI.Wrong code reading, it's really just the slot that has an error.
-
This post is deleted! -
No this one:
@heedaf said in Class issue in connect definition:
self.pushButton.clicked.connect(Dialog.SB_UP_click)
-
@SGaist wow! I'm loosing it. That is the one I meant to paste. The ...connect... line is also created by PYUIC in pycharm.
-
Late night strikes again ! :-D
I misread the code.Your slot should be something like:
def SB_UP_click(self): print("SB UP") self.ui.label.setText("HI") print("got here")
-
Well I'm seriously confused. I swear I tried what you suggested and it didn't work. But what you posted works and I would really like to know what I did wrong since it is an obvious solution. Thank you for your help.
-
You likely forgot the .ui between the self and .label.
Working with designer based UI requires some getting used to since you have all the code in the same file.
You have the Ui_XXX classes for the aesthetics of your widget (and which is auto generated) and the corresponding XXX that is for the logic you want to apply.