我使用Qt设计器创建了两个不同的窗口,input_window.ui和help_window.ui。这是用于显示输入窗口的python脚本。在输入窗口中,有一个菜单栏(“关于>>帮助”)。单击“帮助”时如何弹出help_window?
这是 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)
答案 0 :(得分:1)
Qt Designer用于以简单的方式实现视图,因此生成的类面向视图,我们的工作是像Ui_MainWindow
和MainWindow
一样实现逻辑,与Ui_Help
类似。在你的情况下,我建议你在构建help_window.ui
时使用Dialog模板,但是如果选择Widget模板没有问题,两者都非常兼容。
一个简单的解决方案是创建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_()
答案 1 :(得分:0)
你没有包含input_window.ui所以很难完全复制你正在做的事情,但我认为你遇到的主要问题来自于这一行:
self.Pophelp.triggered.connect(self.Ui_Help)
您不想将按钮连接到Ui_Help,而是希望将其连接到self.help_window
。
答案 2 :(得分:0)
如果将help_window更改为以下代码,则有效。
def help_window(self):
dialog=QtWidgets.QDialog()
dialog.ui=Ui_Help()
dialog.ui.setupUi(dialog)
dialog.exec_()
dialog.show()