I have used Qt designer to create two different windows, input_window.ui and help_window.ui. Here is the python scripts for showing the input window. In input window, there is a menu bar("About>>Help"). How could it pop up a help_window when "Help" is clicked?
Here is init.py
import sys
from input_window import Ui_MainWindow
from PyQt5.QtWidgets import QMainWindow, QApplication
from help_window import Ui_Help
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.Pophelp.triggered.connect(self.Ui_Help)
def help_window(self):
self.window=Ui_Help()
self.window.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
Here is the code of Ui_Help
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Help(object):
def setupUi(self, Help):
Help.setObjectName("Help")
Help.resize(251, 99)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("logo.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
Help.setWindowIcon(icon)
self.gridLayoutWidget = QtWidgets.QWidget(Help)
self.gridLayoutWidget.setGeometry(QtCore.QRect(9, 9, 231, 81))
self.gridLayoutWidget.setObjectName("gridLayoutWidget")
self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.plainTextEdit = QtWidgets.QPlainTextEdit(self.gridLayoutWidget)
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.plainTextEdit.setFont(font)
self.plainTextEdit.setFrameShape(QtWidgets.QFrame.WinPanel)
self.plainTextEdit.setFrameShadow(QtWidgets.QFrame.Sunken)
self.plainTextEdit.setLineWidth(1)
self.plainTextEdit.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustIgnored)
self.plainTextEdit.setReadOnly(True)
self.plainTextEdit.setObjectName("plainTextEdit")
self.gridLayout.addWidget(self.plainTextEdit, 0, 0, 1, 1)
self.retranslateUi(Help)
QtCore.QMetaObject.connectSlotsByName(Help)
Qt Designer serves to implement the view in a simple way, and therefore the class that generates is oriented to the view, and our job is to implement the logic like you did with Ui_MainWindow
and MainWindow
, similarly you do with Ui_Help
. In your case I recommend that when you have built help_window.ui
you would have used the Dialog template, but if you chose the Widget template there is no problem, both are very compatible.
A simple solution is to create a QDialog
and implement in it the Ui_Help
view as shown below:
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.Pophelp.triggered.connect(self.help_window)
def help_window(self):
# If you pass a parent (self) will block the Main Window,
# and if you do not pass both will be independent,
# I recommend you try both cases.
widget = QDialog(self)
ui=Ui_Help()
ui.setupUi(widget)
widget.exec_()
If in the Ui_Help
you want to implement some logic I recommend creating a class similar to MainWindow
as shown below:
class Help(QDialog, Ui_Help):
def __init__(self, parent=None):
super(Help, self).__init__(parent)
self.setupUi(self)
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.Pophelp.triggered.connect(self.help_window)
def help_window(self):
widget = Help()
widget.exec_()