单击菜单并打开新窗口



我使用Qt designer创建了两个不同的窗口,input_window.ui和help_window.ui。这是用于显示输入窗口的 python 脚本。在输入窗口中,有一个菜单栏("关于>>帮助"(。单击"帮助"时如何弹出help_window?

这是初始化.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用于以简单的方式实现视图,因此生成的类面向视图,我们的工作是像Ui_MainWindowMainWindow一样实现逻辑,就像你对Ui_Help所做的那样。在您的情况下,我建议您在构建help_window.ui时使用对话框模板,但是如果您选择小部件模板没有问题,两者都非常兼容。

一个简单的解决方案是创建一个QDialog并在其中实现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.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_()

您没有包含 input_window.ui,因此很难完全复制您正在做的事情,但我认为您遇到的主要问题源于这一行:

self.Pophelp.triggered.connect(self.Ui_Help)

您不想将按钮连接到Ui_Help,而是希望将其连接到self.help_window

如果将help_window更改为下面的代码,则可以正常工作。

def help_window(self):
dialog=QtWidgets.QDialog()
dialog.ui=Ui_Help()
dialog.ui.setupUi(dialog)
dialog.exec_()
dialog.show()

相关内容

  • 没有找到相关文章

最新更新