Related
I created a simple file called (first page) through a qt designer program, and put a button named (login) in it, and I also created another simple file with the same program called (second page)
After running the first file and pressing the button in it, I want to open the second file, or in other words I want to link the pages together.
How can I do this?. Please i need help.
The first file code :
from PyQt5.QtWidgets import QDialog, QApplication
from PyQt5 import QtCore, QtGui, QtWidgets,uic
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(170, 140, 91, 31))
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(12)
font.setBold(True)
font.setItalic(True)
font.setWeight(75)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Login"))
self.pushButton.clicked.connect(self.buttonClicked)
def buttonClicked(self,Ui):
uic.loadUi('secondpage.ui',self)
if name == "main":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
first page file
second page file
I know what you mean. So, there are solution:
from PyQt5.QtWidgets import QDialog, QApplication from PyQt5 import QtCore, QtGui, QtWidgets,uic
# Add second window import here
from second_file import Second_Ui_MainWindow
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(170, 140, 91, 31))
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(12)
font.setBold(True)
font.setItalic(True)
font.setWeight(75)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
# Connect button with function
self.pushButton,clicked.connect(lambda x: self.buttonClicked(MainWindow))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Login"))
self.pushButton.clicked.connect(self.buttonClicked)
def buttonClicked(self, MainWindow):
SecondScreen_MW = QtWidgets.QMainWindow()
ui = Second_Ui_MainWindow()
ui.setupUi(SecondScreen_MW )
SecondScreen_MW.show()
# (Optional) Hide first screen
MainWindow.hide()
But, as said #musicamante : "You should not try to edit pyuic files, but instead use them as explained in using Designer. Also, even when using the proper way to load the UI, you should not use loadUi on an already set widget."
This method works, but it is not the best. If you need a consultation, you can contact me
I have created two simple windows using qt5 designer. In first window there is a button which opens the second window. When the second window opens the button in it which prints a simple text is not working. I have read other post but no one has used qt5 designer!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
self.window = MainWindow
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(290, 230, 181, 29))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.opensecondwindow)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
# QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "First Page"))
self.pushButton.setText(_translate("MainWindow", "Open Second Page"))
def opensecondwindow(self):
import secondpage as scp
ui = scp.Ui_MainWindow()
ui.setupUi(self.window)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(280, 240, 211, 71))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.openfirstwindow)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Second Page"))
self.pushButton.setText(_translate("MainWindow", "Print Data"))
def openfirstwindow(self):
print("Hi from second window")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
any help would be appreciated!
thanks in advance
First, you two classes have the same name Ui_MainWindow.
One option to print what you want might look like this:
from PyQt5 import QtCore, QtGui, QtWidgets
# class Ui_MainWindow(object):
class Ui_MainWindow_First(object): # + _First
def setupUi(self, MainWindow):
# self.window = MainWindow
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(290, 230, 181, 29))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.opensecondwindow)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
# QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "First Page"))
self.pushButton.setText(_translate("MainWindow", "Open Second Page"))
def opensecondwindow(self):
# import secondpage as scp
# ui = scp.Ui_MainWindow()
# self.ui = Ui_MainWindow_Second()
### vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
self.w = QtWidgets.QMainWindow()
w_ui = Ui_MainWindow_Second()
w_ui.setupUi(self.w)
self.w.show()
w_ui.pushButton.clicked.connect(self.openfirstwindow)
def openfirstwindow(self):
print("Hi from second window")
### ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# class Ui_MainWindow(object):
class Ui_MainWindow_Second(object): # + _Second
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(280, 240, 211, 71))
self.pushButton.setObjectName("pushButton")
# self.pushButton.clicked.connect(self.openfirstwindow)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Second Page"))
self.pushButton.setText(_translate("MainWindow", "Print Data"))
# def openfirstwindow(self): #(self):
# print("Hi from second window")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
# ui = Ui_MainWindow()
ui = Ui_MainWindow_First() # Ui_MainWindow_First
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
How to reposition the widgets when window maximized?
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(350, 100, 81, 21))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(270, 130, 81, 21))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(270, 160, 81, 21))
self.label_3.setObjectName("label_3")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(370, 130, 113, 20))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(370, 160, 113, 20))
self.lineEdit_2.setObjectName("lineEdit_2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "TextLabel"))
self.label_2.setText(_translate("MainWindow", "TextLabel"))
self.label_3.setText(_translate("MainWindow", "TextLabel"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
You can read about layout management in PyQt5 here: http://zetcode.com/gui/pyqt5/layout/
Try as an option:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(350, 100, 81, 21))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(270, 130, 81, 21))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(270, 160, 81, 21))
self.label_3.setObjectName("label_3")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(370, 130, 113, 20))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(370, 160, 113, 20))
self.lineEdit_2.setObjectName("lineEdit_2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "TextLabel"))
self.label_2.setText(_translate("MainWindow", "TextLabel"))
self.label_3.setText(_translate("MainWindow", "TextLabel"))
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
formLayout = QtWidgets.QFormLayout()
formLayout.addRow(self.label_2, self.lineEdit)
formLayout.addRow(self.label_3, self.lineEdit_2)
layoutV = QtWidgets.QVBoxLayout(self.centralwidget)
layoutV.addWidget(self.label, alignment=QtCore.Qt.AlignCenter)
layoutV.addLayout(formLayout)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
I have the parallel thread that read 10 uart and prints the data in pyqt5 gui. But the issue I am facing is those groupbox get updated when all 10 jobs completed but not instantly.
EDIT: To achieve parallelism I am using joblib not pyqt thread.
What I want to achieve is change the state of groubox attributes such as a label, pushbutton text, and colour but I don't find a good example how to trigger a signal emit for the groupbox.
Do I need to emit the individual label, buttons in the groubox or I can emit as one groupbox object?
This is a minimalistic code, where it has just two groupbx. Can someone help how to emit the signal from groubox and catch that change, i.e the state of label, button text, and colour in the below code?
Below example update the groubox without any issue but in real each g1 method is uart and it's blocking call. I wait till all jobs to complete to see the changes on my GUI.
Code
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt, pyqtSignal
class Ui_MainWindow(object):
changedValue = pyqtSignal(int)
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox.setGeometry(QtCore.QRect(100, 140, 120, 141))
self.groupBox.setObjectName("groupBox")
self.pushButton_2 = QtWidgets.QPushButton(self.groupBox)
self.pushButton_2.setGeometry(QtCore.QRect(0, 40, 113, 32))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self.groupBox)
self.pushButton_3.setGeometry(QtCore.QRect(0, 90, 113, 32))
self.pushButton_3.setObjectName("pushButton_3")
self.label = QtWidgets.QLabel(self.groupBox)
self.label.setGeometry(QtCore.QRect(0, 20, 121, 16))
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(180, 80, 113, 32))
self.pushButton.setObjectName("pushButton")
self.groupBox_2 = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox_2.setGeometry(QtCore.QRect(230, 140, 120, 141))
self.groupBox_2.setObjectName("groupBox_2")
self.pushButton_4 = QtWidgets.QPushButton(self.groupBox_2)
self.pushButton_4.setGeometry(QtCore.QRect(0, 40, 113, 32))
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_5 = QtWidgets.QPushButton(self.groupBox_2)
self.pushButton_5.setGeometry(QtCore.QRect(0, 90, 113, 32))
self.pushButton_5.setObjectName("pushButton_5")
self.label_2 = QtWidgets.QLabel(self.groupBox_2)
self.label_2.setGeometry(QtCore.QRect(0, 20, 121, 16))
self.label_2.setObjectName("label_2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.pushButton.clicked.connect(self.groupBox.update)
self.pushButton.clicked.connect(self.groupBox_2.update)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.groupBox.setTitle(_translate("MainWindow", "UART1"))
self.pushButton_2.setText(_translate("MainWindow", "Test1"))
self.pushButton_3.setText(_translate("MainWindow", "Test2"))
self.label.setText(_translate("MainWindow", "IMEI"))
self.pushButton.setText(_translate("MainWindow", "START"))
self.groupBox_2.setTitle(_translate("MainWindow", "UART2"))
self.pushButton_4.setText(_translate("MainWindow", "Test1"))
self.pushButton_5.setText(_translate("MainWindow", "Test2"))
self.label_2.setText(_translate("MainWindow", "IMEI"))
#Setting a connection here
self.pushButton.clicked.connect(self.g1)
self.pushButton.clicked.connect(self.g2)
def g1(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
self.pushButton.setText(_translate("MainWindow", "Running"))
imei = "12345"
self.label.setText(imei)
self.changedValue.emit(imei)
self.pushButton_2.setText(_translate("MainWindow", "Pass"))
self.pushButton_3.setText(_translate("MainWindow", "FAIL"))
self.pushButton_3.setStyleSheet("background-color: rgb(0, 255, 0);")
def g2(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
self.pushButton.setText(_translate("MainWindow", "Done"))
imei = "12345"
self.label_2.setText("12345")
self.changedValue.emit(imei)
self.pushButton_4.setText(_translate("MainWindow", "FAIL"))
self.pushButton_4.setStyleSheet("background-color: rgb(0, 255, 0);")
self.pushButton_5.setText(_translate("MainWindow", "Pass"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
First of all I recommend you read Using the Generated Code from the docs. PyQt in this post recommends not modifying the code generated by Qt Designer since it is not a widget it is only a class that is used to fill a widget, instead it is better to create another class based on the chosen form and implement the logic there, since for example your code has a serious error: the signals can only be created in a QObject like the widgets but Ui_MainWindow is not.
Considering the above, the solution is:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox.setGeometry(QtCore.QRect(100, 140, 120, 141))
self.groupBox.setObjectName("groupBox")
self.pushButton_2 = QtWidgets.QPushButton(self.groupBox)
self.pushButton_2.setGeometry(QtCore.QRect(0, 40, 113, 32))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self.groupBox)
self.pushButton_3.setGeometry(QtCore.QRect(0, 90, 113, 32))
self.pushButton_3.setObjectName("pushButton_3")
self.label = QtWidgets.QLabel(self.groupBox)
self.label.setGeometry(QtCore.QRect(0, 20, 121, 16))
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(180, 80, 113, 32))
self.pushButton.setObjectName("pushButton")
self.groupBox_2 = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox_2.setGeometry(QtCore.QRect(230, 140, 120, 141))
self.groupBox_2.setObjectName("groupBox_2")
self.pushButton_4 = QtWidgets.QPushButton(self.groupBox_2)
self.pushButton_4.setGeometry(QtCore.QRect(0, 40, 113, 32))
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_5 = QtWidgets.QPushButton(self.groupBox_2)
self.pushButton_5.setGeometry(QtCore.QRect(0, 90, 113, 32))
self.pushButton_5.setObjectName("pushButton_5")
self.label_2 = QtWidgets.QLabel(self.groupBox_2)
self.label_2.setGeometry(QtCore.QRect(0, 20, 121, 16))
self.label_2.setObjectName("label_2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.pushButton.clicked.connect(self.groupBox.update)
self.pushButton.clicked.connect(self.groupBox_2.update)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.groupBox.setTitle(_translate("MainWindow", "UART1"))
self.pushButton_2.setText(_translate("MainWindow", "Test1"))
self.pushButton_3.setText(_translate("MainWindow", "Test2"))
self.label.setText(_translate("MainWindow", "IMEI"))
self.pushButton.setText(_translate("MainWindow", "START"))
self.groupBox_2.setTitle(_translate("MainWindow", "UART2"))
self.pushButton_4.setText(_translate("MainWindow", "Test1"))
self.pushButton_5.setText(_translate("MainWindow", "Test2"))
self.label_2.setText(_translate("MainWindow", "IMEI"))
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
changedValue = QtCore.pyqtSignal(int)
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
#Setting a connection here
self.pushButton.clicked.connect(self.g1)
self.pushButton.clicked.connect(self.g2)
#QtCore.pyqtSlot()
def g1(self):
_translate = QtCore.QCoreApplication.translate
self.pushButton.setText(_translate("MainWindow", "Running"))
imei = "12345"
self.label.setText(imei)
self.changedValue.emit(imei)
self.pushButton_2.setText("Pass")
self.pushButton_3.setText(_translate("MainWindow", "FAIL"))
self.pushButton_3.setStyleSheet("background-color: rgb(0, 255, 0);")
#QtCore.pyqtSlot()
def g2(self):
_translate = QtCore.QCoreApplication.translate
self.pushButton.setText(_translate("MainWindow", "Done"))
imei = "12345"
self.label_2.setText("12345")
self.changedValue.emit(imei)
self.pushButton_4.setText(_translate("MainWindow", "FAIL"))
self.pushButton_4.setStyleSheet("background-color: rgb(0, 255, 0);")
self.pushButton_5.setText(_translate("MainWindow", "Pass"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
I am writing GUI app with the help of Qt Designer. The app consists of multiple windows, so when the user clicks the push button on one window, the next window should open and the current window should hide. I achieved this feature for going from window 1 to window 2. But I am not able to do this to move from window 2 to window 3. I think this might be because the window 2 gets user arguments.
Is there any way to hide the window which gets user arguments?
The code is as follows:
Class for main window:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
import user_data
class MainWindow(object):
def user_data(self):
self.user_data_window = QtWidgets.QMainWindow()
self.next_window = user_data.Ui_UserData()
self.next_window.setupUi(self.user_data_window)
main_window.hide()
self.user_data_window.show()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(640, 480)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(140, 100, 361, 81))
font = QtGui.QFont()
font.setPointSize(24)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(260, 260, 75, 51))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.user_data)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 640, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Main Window"))
self.label.setText(_translate("MainWindow", " Manager"))
self.pushButton.setText(_translate("MainWindow", "Lets Start!"))
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
main_window = QtWidgets.QMainWindow()
ui = MainWindow()
ui.setupUi(main_window)
main_window.show()
sys.exit(app.exec_())
user_data.py ---> window 2
from PyQt5 import QtCore, QtGui, QtWidgets
import start_recording
class Ui_UserData(object):
def start_record(self):
self.window = QtWidgets.QMainWindow()
self.ui = start_recording.Ui_StartRecording()
self.ui.setupUi(self.window)
"""
This is the part not working
"""
MainWindow.hide()
self.window.show()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(640, 480)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.groupBox_speaker_data = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox_speaker_data.setGeometry(QtCore.QRect(80, 30, 481, 381))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(84)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
self.groupBox_speaker_data.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.groupBox_speaker_data.setFont(font)
self.groupBox_speaker_data.setObjectName("groupBox_speaker_data")
self.label_name = QtWidgets.QLabel(self.groupBox_speaker_data)
self.label_name.setGeometry(QtCore.QRect(30, 60, 47, 13))
self.label_name.setObjectName("label_name")
self.label_age = QtWidgets.QLabel(self.groupBox_speaker_data)
self.label_age.setGeometry(QtCore.QRect(30, 120, 47, 13))
self.label_age.setObjectName("label_age")
self.label_id = QtWidgets.QLabel(self.groupBox_speaker_data)
self.label_id.setGeometry(QtCore.QRect(30, 90, 47, 13))
self.label_id.setObjectName("label_id")
self.label_gender = QtWidgets.QLabel(self.groupBox_speaker_data)
self.label_gender.setGeometry(QtCore.QRect(30, 150, 47, 13))
self.label_gender.setObjectName("label_gender")
self.lineEdit_name = QtWidgets.QLineEdit(self.groupBox_speaker_data)
self.lineEdit_name.setGeometry(QtCore.QRect(110, 60, 141, 20))
self.lineEdit_name.setObjectName("lineEdit_name")
self.lineEdit_id = QtWidgets.QLineEdit(self.groupBox_speaker_data)
self.lineEdit_id.setGeometry(QtCore.QRect(110, 90, 141, 20))
self.lineEdit_id.setObjectName("lineEdit_id")
self.spinBox_age = QtWidgets.QSpinBox(self.groupBox_speaker_data)
self.spinBox_age.setGeometry(QtCore.QRect(110, 120, 42, 22))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
sizePolicy.setHorizontalStretch(18)
sizePolicy.setVerticalStretch(78)
self.spinBox_age.setSizePolicy(sizePolicy)
self.spinBox_age.setMinimum(18)
self.spinBox_age.setObjectName("spinBox_age")
self.comboBox_gender = QtWidgets.QComboBox(self.groupBox_speaker_data)
self.comboBox_gender.setGeometry(QtCore.QRect(110, 150, 69, 22))
self.comboBox_gender.setObjectName("comboBox_gender")
self.comboBox_gender.addItem("")
self.comboBox_gender.addItem("")
self.comboBox_gender.addItem("")
self.pushButton = QtWidgets.QPushButton(self.groupBox_speaker_data)
self.pushButton.setGeometry(QtCore.QRect(370, 330, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.start_record)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 640, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "User Data"))
self.groupBox_speaker_data.setTitle(_translate("MainWindow", "Speaker Data"))
self.label_name.setText(_translate("MainWindow", "Name:"))
self.label_age.setText(_translate("MainWindow", "Age:"))
self.label_id.setText(_translate("MainWindow", "ID:"))
self.label_gender.setText(_translate("MainWindow", "Gender:"))
self.comboBox_gender.setItemText(0, _translate("MainWindow", "Male"))
self.comboBox_gender.setItemText(1, _translate("MainWindow", "Female"))
self.comboBox_gender.setItemText(2, _translate("MainWindow", "Unknown"))
self.pushButton.setText(_translate("MainWindow", "NEXT"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_UserData()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
start_recording.py ----> window 3
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_StartRecording(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(640, 480)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(160, 120, 331, 91))
font = QtGui.QFont()
font.setFamily("Arial Black")
font.setPointSize(18)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(260, 200, 101, 51))
font = QtGui.QFont()
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(260, 260, 75, 23))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 640, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "Lets begin"))
self.label_2.setText(_translate("MainWindow", "xxx!"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_StartRecording()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
class Ui_UserData(object):
def start_record(self):
self.window = QtWidgets.QMainWindow()
self.ui = start_recording.Ui_StartRecording()
self.ui.setupUi(self.window)
"""
This is the part not working
"""
MainWindow.hide()
^^^^^^^^ ---- there is no 'MainWindow' variable available to class, hence it doesn't know what to hide here.
Solution is:
class Ui_UserData(object):
def start_record(self):
self.window = QtWidgets.QMainWindow()
self.ui = start_recording.Ui_StartRecording()
self.ui.setupUi(self.window)
"""
This part is now working!
"""
self.mw.hide()
self.window.show()
def setupUi(self, MainWindow):
self.mw = MainWindow
# ^^^^^^^^ - You create class variable for mainwindow object