[英]PyQt5, click menu and open new window
I have used Qt designer to create two different windows, input_window.ui and help_window.ui. 我使用Qt设计器来创建两个不同的窗口,input_window.ui和help_window.ui。 Here is the python scripts for showing the input window. 这是用于显示输入窗口的python脚本。 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 这是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 这是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
. Qt设计用于实现以简单的方式的观点,并因此产生了类是面向来看,我们的工作就是实现逻辑像你这样有Ui_MainWindow
和MainWindow
,同样你做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. 在您的情况下,我建议您在构建help_window.ui
使用Dialog模板,但是如果您选择Widget模板则没有问题,两者都非常兼容。
A simple solution is to create a QDialog
and implement in it the Ui_Help
view as shown below: 一个简单的解决方案是创建一个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_()
If in the Ui_Help
you want to implement some logic I recommend creating a class similar to MainWindow
as shown below: 如果要在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_()
You didn't include input_window.ui so it's hard to fully replicate what you are doing, but I think the main problem you have is stemming from this line: 您没有包含input_window.ui,因此很难完全复制您正在做的事情,但是我认为您的主要问题出在此行:
self.Pophelp.triggered.connect(self.Ui_Help)
You don't want to connect the button to Ui_Help, you want to connect it to self.help_window
. 您不想将按钮连接到Ui_Help,而是要将其连接到self.help_window
。
It works if change the help_window to the code below. 如果将help_window更改为以下代码,则可以使用。
def help_window(self):
dialog=QtWidgets.QDialog()
dialog.ui=Ui_Help()
dialog.ui.setupUi(dialog)
dialog.exec_()
dialog.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.