gpt4 book ai didi

python - 单击菜单并打开新窗口

转载 作者:行者123 更新时间:2023-12-02 03:50:09 27 4
gpt4 key购买 nike

我使用 Qt 设计器创建了两个不同的窗口,input_window.ui 和 help_window.ui。这是用于显示输入窗口的 python 脚本。在输入窗口中,有一个菜单栏(“关于>>帮助”)。点击“Help”时怎么会弹出help_window?

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_())

这是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 以简单的方式实现 View ,因此生成的类是面向 View 的,我们的工作是实现像 Ui_MainWindow 中那样的逻辑>MainWindow,与 Ui_Help 类似。对于您的情况,我建议您在构建 help_window.ui 时使用 Dialog 模板,但如果您选择 Widget 模板则没有问题,两者都非常兼容。

一个简单的解决方案是创建一个 QDialog 并在其中实现 Ui_Help View ,如下所示:

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 的类,如下所示:

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_()

关于python - 单击菜单并打开新窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45688873/

27 4 0