Issue
How to use pyqtSlot to connect the QMainWindow with 3 QtWidgets?
I wanna to use 3 buttons to switch 3 screen for each .py files.
The UiPE.py file for the 3 pushbutton to switch 3 screens of .py file.
#=============
**UiPE.py**
#=============
# -*- coding: utf-8 -*-
"""
Module implementing PB01.
"""
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QMainWindow
from Ui_UiPE import Ui_MainWindow
class PB01(QMainWindow, Ui_MainWindow):
"""
Class documentation goes here.
"""
def __init__(self, parent=None):
"""
Constructor
@param parent reference to the parent widget
@type QWidget
"""
super(PB01, self).__init__(parent)
self.setupUi(self)
@pyqtSlot()
def on_PBtn1PE_clicked(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
#raise NotImplementedError
@pyqtSlot()
def on_PBtn2LC_clicked(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
raise NotImplementedError
@pyqtSlot()
def on_PBtn3PP_clicked(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
raise NotImplementedError
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
FormPE = QtWidgets.QWidget()
ui = Ui_FormPE()
ui.setupUi(FormPE)
FormPE.show()
sys.exit(app.exec_())
Ui_UiPE.py
#=============
**Ui_UiPE.py**
#=============
# -*- coding: utf-8 -*-
/*Form implementation generated from reading ui file
'..\PyQt5\eee\UiPE.ui'*/
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(960, 736)
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setStyleSheet("border-image:\
url(:/ME/images/Main.jpg);")
self.centralWidget.setObjectName("centralWidget")
self.frame = QtWidgets.QFrame(self.centralWidget)
self.frame.setGeometry(QtCore.QRect(0, 0, 960, 500))
self.frame.setAutoFillBackground(False)
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.PBtn1PE = QtWidgets.QPushButton(self.frame)
self.PBtn1PE.setEnabled(True)
self.PBtn1PE.setGeometry(QtCore.QRect(150, 400, 150, 50))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(18)
font.setBold(False)
font.setWeight(50)
self.PBtn1PE.setFont(font)
self.PBtn1PE.setStyleSheet("border:2px groove gray;\
border-radius:10px;padding:2px 4px;background-color: \
red;border-image: url();")
self.PBtn1PE.setText("PE")
self.PBtn1PE.setDefault(False)
self.PBtn1PE.setObjectName("PBtn1PE")
self.PBtn2LC = QtWidgets.QPushButton(self.frame)
self.PBtn2LC.setGeometry(QtCore.QRect(400, 400, 150, 50))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(18)
font.setBold(False)
font.setWeight(50)
self.PBtn2LC.setFont(font)
self.PBtn2LC.setStyleSheet("border:2px groove gray;\
border-radius:10px;padding:2px 4px;background-color: \
yellow;border-image: url();")
self.PBtn2LC.setObjectName("PBtn2LC")
self.PBtn3PP = QtWidgets.QPushButton(self.frame)
self.PBtn3PP.setGeometry(QtCore.QRect(650, 400, 150, 50))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(18)
font.setBold(False)
font.setWeight(50)
self.PBtn3PP.setFont(font)
self.PBtn3PP.setAutoFillBackground(False)
self.PBtn3PP.setStyleSheet("border:2px groove gray;\
border-radius:10px;padding:2px 4px;background-color: \
rgb(0, 162, 232);;border-image: url();")
self.PBtn3PP.setObjectName("PBtn3PP")
self.BtnClose = QtWidgets.QPushButton(self.frame)
self.BtnClose.setGeometry(QtCore.QRect(860, 40, 81, 41))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.BtnClose.setFont(font)
self.BtnClose.setWhatsThis("")
self.BtnClose.setAccessibleName("")
self.BtnClose.setAutoFillBackground(False)
self.BtnClose.setStyleSheet("border:2px groove gray;\
border-radius:10px;padding:2px 4px;background-color: red;\
border-image: url();")
self.BtnClose.setText("Exit")
self.BtnClose.setObjectName("BtnClose")
MainWindow.setCentralWidget(self.centralWidget)
self.retranslateUi(MainWindow)
self.PBtn1PE.released.connect(MainWindow.show)
self.BtnClose.clicked.connect(self.frame.close)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
MainWindow.setTabOrder(self.PBtn1PE, self.PBtn2LC)
MainWindow.setTabOrder(self.PBtn2LC, self.PBtn3PP)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.PBtn2LC.setText(_translate("MainWindow", "LC"))
self.PBtn3PP.setText(_translate("MainWindow", "PP"))
#import MainE_rc
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_())
Ui_UiPE1.py
/*=============
#**Ui_UiPE1.py**, **Ui_UiLC.py** and **Ui_UiPP.py** is same script for
#differet function.
=============*/
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file \
#'C:\Users\ST3C_01\PyQt5\eee\UiPE1.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_FormPE(object):
def setupUi(self, FormPE):
FormPE.setObjectName("FormPE")
FormPE.resize(960, 500)
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(16)
FormPE.setFont(font)
FormPE.setStyleSheet("border-image:\
url(:/myframe/images/PresidentElection.jpg);")
self.PEBtn1B = QtWidgets.QPushButton(FormPE)
self.PEBtn1B.setGeometry(QtCore.QRect(540, 420, 111, 40))
self.PEBtn1B.setMaximumSize(QtCore.QSize(120, 40))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(18)
font.setBold(True)
font.setWeight(75)
self.PEBtn1B.setFont(font)
self.PEBtn1B.setLayoutDirection(QtCore.Qt.RightToLeft)
self.PEBtn1B.setText(" Decrease")
self.PEBtn1B.setObjectName("PEBtn1B")
self.PEBtn1A = QtWidgets.QPushButton(FormPE)
self.PEBtn1A.setGeometry(QtCore.QRect(550, 30, 111, 40))
self.PEBtn1A.setMaximumSize(QtCore.QSize(120, 40))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(18)
font.setBold(True)
font.setWeight(75)
self.PEBtn1A.setFont(font)
self.PEBtn1A.setLayoutDirection(QtCore.Qt.RightToLeft)
self.PEBtn1A.setText(" Increase")
self.PEBtn1A.setObjectName("PEBtn1A")
self.PEBtn2A = QtWidgets.QPushButton(FormPE)
self.PEBtn2A.setGeometry(QtCore.QRect(690, 30, 111, 40))
self.PEBtn2A.setMaximumSize(QtCore.QSize(120, 40))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(18)
font.setBold(True)
font.setWeight(75)
self.PEBtn2A.setFont(font)
self.PEBtn2A.setLayoutDirection(QtCore.Qt.RightToLeft)
self.PEBtn2A.setText(" Increase")
self.PEBtn2A.setObjectName("PEBtn2A")
self.PEBtn3A = QtWidgets.QPushButton(FormPE)
self.PEBtn3A.setGeometry(QtCore.QRect(830, 30, 111, 40))
self.PEBtn3A.setMaximumSize(QtCore.QSize(120, 40))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(18)
font.setBold(True)
font.setWeight(75)
self.PEBtn3A.setFont(font)
self.PEBtn3A.setLayoutDirection(QtCore.Qt.RightToLeft)
self.PEBtn3A.setText(" Increase")
self.PEBtn3A.setObjectName("PEBtn3A")
self.PEBtn2B = QtWidgets.QPushButton(FormPE)
self.PEBtn2B.setGeometry(QtCore.QRect(690, 420, 111, 40))
self.PEBtn2B.setMaximumSize(QtCore.QSize(120, 40))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(18)
font.setBold(True)
font.setWeight(75)
self.PEBtn2B.setFont(font)
self.PEBtn2B.setLayoutDirection(QtCore.Qt.RightToLeft)
self.PEBtn2B.setText(" Decrease")
self.PEBtn2B.setObjectName("PEBtn2B")
self.PEBtn3B = QtWidgets.QPushButton(FormPE)
self.PEBtn3B.setGeometry(QtCore.QRect(830, 420, 111, 40))
self.PEBtn3B.setMaximumSize(QtCore.QSize(120, 40))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(18)
font.setBold(True)
font.setWeight(75)
self.PEBtn3B.setFont(font)
self.PEBtn3B.setLayoutDirection(QtCore.Qt.RightToLeft)
self.PEBtn3B.setText(" Decrease")
self.PEBtn3B.setObjectName("PEBtn3B")
self.retranslateUi(FormPE)
QtCore.QMetaObject.connectSlotsByName(FormPE)
def retranslateUi(self, FormPE):
_translate = QtCore.QCoreApplication.translate
FormPE.setWindowTitle(_translate("FormPE", "Form"))
#import PE_rc
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
FormPE = QtWidgets.QWidget()
ui = Ui_FormPE()
ui.setupUi(FormPE)
FormPE.show()
sys.exit(app.exec_())
The above is my example.
Solution
Try it:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QMainWindow
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
# MainWindow.resize(960, 536)
MainWindow.setFixedSize(960, 470) # +
self.centralWidget = QtWidgets.QWidget(MainWindow)
# self.centralWidget.setStyleSheet("border-image: url(Ok.png);")
self.centralWidget.setObjectName("centralWidget") # +++
self.centralWidget.setStyleSheet("#centralWidget {border-image: url(Ok.png);}") # +++
self.frame = QtWidgets.QFrame(self.centralWidget)
self.frame.setGeometry(QtCore.QRect(0, 0, 960, 500))
self.frame.setAutoFillBackground(False)
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.PBtn1PE = QtWidgets.QPushButton(self.frame)
self.PBtn1PE.setEnabled(True)
self.PBtn1PE.setGeometry(QtCore.QRect(150, 400, 150, 50))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(18)
font.setBold(False)
font.setWeight(50)
self.PBtn1PE.setFont(font)
self.PBtn1PE.setStyleSheet("""
border: 2px groove gray;
border-radius: 10px;
padding: 2px 4px;
background-color: red;
""")
self.PBtn1PE.setText("PE")
self.PBtn1PE.setDefault(False)
self.PBtn1PE.setObjectName("PBtn1PE")
self.PBtn2LC = QtWidgets.QPushButton(self.frame)
self.PBtn2LC.setGeometry(QtCore.QRect(400, 400, 150, 50))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(18)
font.setBold(False)
font.setWeight(50)
self.PBtn2LC.setFont(font)
self.PBtn2LC.setStyleSheet("border:2px groove gray;\
border-radius:10px;padding:2px 4px;background-color: yellow;")
self.PBtn2LC.setObjectName("PBtn2LC")
self.PBtn3PP = QtWidgets.QPushButton(self.frame)
self.PBtn3PP.setGeometry(QtCore.QRect(650, 400, 150, 50))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(18)
font.setBold(False)
font.setWeight(50)
self.PBtn3PP.setFont(font)
self.PBtn3PP.setAutoFillBackground(False)
self.PBtn3PP.setStyleSheet("border:2px groove gray;\
border-radius:10px;padding:2px 4px;background-color: rgb(0, 162, 232);")
self.PBtn3PP.setObjectName("PBtn3PP")
self.BtnClose = QtWidgets.QPushButton(self.frame)
self.BtnClose.setGeometry(QtCore.QRect(860, 40, 81, 41))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.BtnClose.setFont(font)
self.BtnClose.setWhatsThis("")
self.BtnClose.setAccessibleName("")
self.BtnClose.setAutoFillBackground(False)
self.BtnClose.setStyleSheet("border:2px groove gray;\
border-radius:10px;padding:2px 4px;background-color: red;")
self.BtnClose.setText("Exit")
self.BtnClose.setObjectName("BtnClose")
MainWindow.setCentralWidget(self.centralWidget)
self.retranslateUi(MainWindow)
self.PBtn1PE.released.connect(MainWindow.show)
self.BtnClose.clicked.connect(self.frame.close)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
MainWindow.setTabOrder(self.PBtn1PE, self.PBtn2LC)
MainWindow.setTabOrder(self.PBtn2LC, self.PBtn3PP)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.PBtn2LC.setText(_translate("MainWindow", "LC"))
self.PBtn3PP.setText(_translate("MainWindow", "PP"))
class Ui_FormPE(object):
def setupUi(self, FormPE):
FormPE.setObjectName("FormPE")
FormPE.resize(960, 500)
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(16)
FormPE.setFont(font)
FormPE.setStyleSheet("#FormPE {border-image: url(im.png);}") # + #FormPE
self.PEBtn1B = QtWidgets.QPushButton(FormPE)
self.PEBtn1B.setGeometry(QtCore.QRect(540, 420, 111, 40))
self.PEBtn1B.setMaximumSize(QtCore.QSize(120, 40))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(16) # 16
font.setBold(True)
font.setWeight(75)
self.PEBtn1B.setFont(font)
self.PEBtn1B.setLayoutDirection(QtCore.Qt.RightToLeft)
self.PEBtn1B.setText(" Decrease ") # " Decrease "
self.PEBtn1B.setObjectName("PEBtn1B")
self.PEBtn1A = QtWidgets.QPushButton(FormPE)
self.PEBtn1A.setGeometry(QtCore.QRect(550, 30, 111, 40))
self.PEBtn1A.setMaximumSize(QtCore.QSize(120, 40))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.PEBtn1A.setFont(font)
self.PEBtn1A.setLayoutDirection(QtCore.Qt.RightToLeft)
self.PEBtn1A.setText(" Increase ")
self.PEBtn1A.setObjectName("PEBtn1A")
self.PEBtn2A = QtWidgets.QPushButton(FormPE)
self.PEBtn2A.setGeometry(QtCore.QRect(690, 30, 111, 40))
self.PEBtn2A.setMaximumSize(QtCore.QSize(120, 40))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.PEBtn2A.setFont(font)
self.PEBtn2A.setLayoutDirection(QtCore.Qt.RightToLeft)
self.PEBtn2A.setText(" Increase ")
self.PEBtn2A.setObjectName("PEBtn2A")
self.PEBtn3A = QtWidgets.QPushButton(FormPE)
self.PEBtn3A.setGeometry(QtCore.QRect(830, 30, 111, 40))
self.PEBtn3A.setMaximumSize(QtCore.QSize(120, 40))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.PEBtn3A.setFont(font)
self.PEBtn3A.setLayoutDirection(QtCore.Qt.RightToLeft)
self.PEBtn3A.setText(" Increase ")
self.PEBtn3A.setObjectName("PEBtn3A")
self.PEBtn2B = QtWidgets.QPushButton(FormPE)
self.PEBtn2B.setGeometry(QtCore.QRect(690, 420, 111, 40))
self.PEBtn2B.setMaximumSize(QtCore.QSize(120, 40))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.PEBtn2B.setFont(font)
self.PEBtn2B.setLayoutDirection(QtCore.Qt.RightToLeft)
self.PEBtn2B.setText(" Decrease ")
self.PEBtn2B.setObjectName("PEBtn2B")
self.PEBtn3B = QtWidgets.QPushButton(FormPE)
self.PEBtn3B.setGeometry(QtCore.QRect(830, 420, 111, 40))
self.PEBtn3B.setMaximumSize(QtCore.QSize(120, 40))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.PEBtn3B.setFont(font)
self.PEBtn3B.setLayoutDirection(QtCore.Qt.RightToLeft)
self.PEBtn3B.setText(" Decrease ")
self.PEBtn3B.setObjectName("PEBtn3B")
self.retranslateUi(FormPE)
QtCore.QMetaObject.connectSlotsByName(FormPE)
def retranslateUi(self, FormPE):
_translate = QtCore.QCoreApplication.translate
FormPE.setWindowTitle(_translate("FormPE", "Form"))
class PB01(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(PB01, self).__init__(parent)
self.setupUi(self)
@pyqtSlot()
def on_PBtn1PE_clicked(self):
print("on_PBtn1PE_clicked")
self.FormPE = QtWidgets.QWidget()
ui = Ui_FormPE()
ui.setupUi(self.FormPE)
self.FormPE.show()
@pyqtSlot()
def on_PBtn2LC_clicked(self):
print("on_PBtn2LC_clicked")
@pyqtSlot()
def on_PBtn3PP_clicked(self):
print("on_PBtn3PP_clicked")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = PB01() # +++
w.show() # +++
# FormPE = QtWidgets.QWidget()
# ui = Ui_FormPE()
# ui.setupUi(FormPE)
# FormPE.show()
sys.exit(app.exec_())
Answered By - S. Nick
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.