PyQt5,单击菜单并打开新窗口 [英] PyQt5, click menu and open new window
问题描述
我使用 Qt 设计器创建了两个不同的窗口,input_window.ui 和 help_window.ui.这是用于显示输入窗口的 python 脚本.在输入窗口中,有一个菜单栏(关于>>帮助").单击帮助"时如何弹出帮助窗口?
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?
这里是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_())
这是Ui_Help的代码
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 以简单的方式实现视图,因此生成的类是面向视图的,我们的工作是实现这样的逻辑你用 Ui_MainWindow
和 MainWindow
做的,类似你用 Ui_Help
做的.在您的情况下,我建议您在构建 help_window.ui
时使用 Dialog 模板,但如果您选择 Widget 模板,则没有问题,两者非常兼容.
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.
一个简单的解决方案是创建一个 QDialog
并在其中实现 Ui_Help
视图,如下所示:
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_()
如果在 Ui_Help
中你想实现一些逻辑,我建议创建一个类似于 MainWindow
的类,如下所示:
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_()
这篇关于PyQt5,单击菜单并打开新窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!