所以,我尝试构建一个应用程序,并努力将动作连接到另一个对话框窗口。我搜索了它,但只找到了一个没有用Qt Designer构建的窗口的解决方案。我使用Qt Designer设计它并将.ui编译为.py格式。
这是我的第一个窗口:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'home.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
from howto import Ui_HowToDialog
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_HomeWindow(object):
def setupUi(self, HomeWindow):
HomeWindow.setObjectName(_fromUtf8("HomeWindow"))
HomeWindow.resize(1106, 259)
font = QtGui.QFont()
font.setFamily(_fromUtf8("Consolas"))
HomeWindow.setFont(font)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(_fromUtf8(":/resources/plottwist.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
HomeWindow.setWindowIcon(icon)
HomeWindow.setLayoutDirection(QtCore.Qt.LeftToRight)
self.centralwidget = QtGui.QWidget(HomeWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.horizontalLayout = QtGui.QHBoxLayout(self.centralwidget)
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.homeBrowser = QtGui.QTextBrowser(self.centralwidget)
font = QtGui.QFont()
font.setFamily(_fromUtf8("Consolas"))
self.homeBrowser.setFont(font)
self.homeBrowser.setObjectName(_fromUtf8("homeBrowser"))
self.horizontalLayout.addWidget(self.homeBrowser)
HomeWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtGui.QStatusBar(HomeWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
HomeWindow.setStatusBar(self.statusbar)
self.homeToolbar = QtGui.QToolBar(HomeWindow)
font = QtGui.QFont()
font.setFamily(_fromUtf8("Consolas"))
font.setPointSize(10)
self.homeToolbar.setFont(font)
self.homeToolbar.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.homeToolbar.setLayoutDirection(QtCore.Qt.LeftToRight)
self.homeToolbar.setToolButtonStyle(QtCore.Qt.ToolButtonTextUnderIcon)
self.homeToolbar.setObjectName(_fromUtf8("homeToolbar"))
HomeWindow.addToolBar(QtCore.Qt.LeftToolBarArea, self.homeToolbar)
self.actionOpenDataset = QtGui.QAction(HomeWindow)
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap(_fromUtf8(":/resources/dataset.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actionOpenDataset.setIcon(icon1)
self.actionOpenDataset.setObjectName(_fromUtf8("actionOpenDataset"))
self.actionHowTo = QtGui.QAction(HomeWindow)
self.actionHowTo.triggered.connect(self.showHowto)
icon2 = QtGui.QIcon()
icon2.addPixmap(QtGui.QPixmap(_fromUtf8(":/resources/howto.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actionHowTo.setIcon(icon2)
self.actionHowTo.setObjectName(_fromUtf8("actionHowTo"))
self.actionAbout = QtGui.QAction(HomeWindow)
icon3 = QtGui.QIcon()
icon3.addPixmap(QtGui.QPixmap(_fromUtf8(":/resources/about.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actionAbout.setIcon(icon3)
self.actionAbout.setObjectName(_fromUtf8("actionAbout"))
self.homeToolbar.addAction(self.actionOpenDataset)
self.homeToolbar.addAction(self.actionHowTo)
self.homeToolbar.addAction(self.actionAbout)
self.retranslateUi(HomeWindow)
QtCore.QMetaObject.connectSlotsByName(HomeWindow)
def retranslateUi(self, HomeWindow):
HomeWindow.setWindowTitle(_translate("HomeWindow", "Plot Twist", None))
self.homeBrowser.setHtml(_translate("HomeWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'Consolas\'; font-size:7.5pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"justify\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:14pt; font-weight:600;\">Data Visualization is as important as it sounds, you can visualize your data with this app just by inserting any dataset you want. For more detailed instructions, simply go to How To icon.</span></p>\n"
"<p align=\"justify\" style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:14pt; font-weight:600;\"><br /></p>\n"
"<p align=\"justify\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt; font-weight:600;\">With Plot Twist, you can:</span></p>\n"
"<p align=\"justify\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt; font-weight:600;\">- Look inside your dataset</span></p>\n"
"<p align=\"justify\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt; font-weight:600;\">- Visualize it by choosing any possible diagrams of any columns</span></p>\n"
"<p align=\"justify\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt; font-weight:600;\">- Find out specific calculation of any column</span></p></body></html>", None))
self.homeToolbar.setWindowTitle(_translate("HomeWindow", "toolBar", None))
self.actionOpenDataset.setText(_translate("HomeWindow", "Open Dataset", None))
self.actionOpenDataset.setStatusTip(_translate("HomeWindow", "Open dataset to visualize it.", None))
self.actionOpenDataset.setShortcut(_translate("HomeWindow", "Ctrl+O", None))
self.actionHowTo.setText(_translate("HomeWindow", "How To", None))
self.actionHowTo.setToolTip(_translate("HomeWindow", "How To Do This?", None))
self.actionHowTo.setStatusTip(_translate("HomeWindow", "Instructions on how this app works.", None))
self.actionHowTo.setShortcut(_translate("HomeWindow", "F1", None))
self.actionAbout.setText(_translate("HomeWindow", "About", None))
self.actionAbout.setToolTip(_translate("HomeWindow", "About", None))
self.actionAbout.setStatusTip(_translate("HomeWindow", "Description and message by creator.", None))
self.actionAbout.setShortcut(_translate("HomeWindow", "F2", None))
def showHowto(self):
HowToDialog = QtGui.QDialog()
hui = Ui_HowToDialog()
hui.setupUi(HowToDialog)
HowToDialog.show()
sys.exit(app.exec_())
import resources_rc
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
HomeWindow = QtGui.QMainWindow()
ui = Ui_HomeWindow()
ui.setupUi(HomeWindow)
HomeWindow.show()
sys.exit(app.exec_())
这是我试图弹出的对话框。当触发工具栏上的图标时,它应该显示HowTo对话框。
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'howto.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_HowToDialog(object):
def setupUi(self, HowToDialog):
HowToDialog.setObjectName(_fromUtf8("HowToDialog"))
HowToDialog.resize(585, 242)
font = QtGui.QFont()
font.setFamily(_fromUtf8("Consolas"))
HowToDialog.setFont(font)
HowToDialog.setAcceptDrops(False)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(_fromUtf8(":/resources/howto.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
HowToDialog.setWindowIcon(icon)
self.horizontalLayout = QtGui.QHBoxLayout(HowToDialog)
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.howtoBrowser = QtGui.QTextBrowser(HowToDialog)
self.howtoBrowser.setObjectName(_fromUtf8("howtoBrowser"))
self.horizontalLayout.addWidget(self.howtoBrowser)
self.retranslateUi(HowToDialog)
QtCore.QMetaObject.connectSlotsByName(HowToDialog)
def retranslateUi(self, HowToDialog):
HowToDialog.setWindowTitle(_translate("HowToDialog", "How To Do This?", None))
self.howtoBrowser.setHtml(_translate("HowToDialog", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'Consolas\'; font-size:7.5pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"justify\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt;\">How to insert your dataset:</span></p>\n"
"<p align=\"justify\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt;\">1. Click </span><span style=\" font-size:12pt; font-weight:600;\">Open Dataset </span><span style=\" font-size:12pt;\">icon.</span></p>\n"
"<p align=\"justify\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt;\">2. Click </span><span style=\" font-size:12pt; font-weight:600;\">Browse </span><span style=\" font-size:12pt;\">and select your dataset.</span></p>\n"
"<p align=\"justify\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt;\">3. Note that the dataset has to be a .csv file.</span></p>\n"
"<p align=\"justify\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt;\">4. Click </span><span style=\" font-size:12pt; font-weight:600;\">Insert File </span><span style=\" font-size:12pt;\">button.</span></p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt;\">5. Your dataset visualization window will appear, and you can start to visualize it.</span></p>\n"
"<p align=\"justify\" style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:12pt;\"><br /></p>\n"
"<p align=\"justify\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt;\">Happy visualizing :)</span></p></body></html>", None))
import resources_rc
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
HowToDialog = QtGui.QDialog()
ui = Ui_HowToDialog()
ui.setupUi(HowToDialog)
HowToDialog.show()
sys.exit(app.exec_())
如您所见,这是我定义的用于显示第二个窗口的函数:
def showHowto(self):
HowToDialog = QtGui.QDialog()
hui = Ui_HowToDialog()
hui.setupUi(HowToDialog)
HowToDialog.show()
sys.exit(app.exec_())
我已将其与self.actionHowTo.triggered.connect(self.showHowto)
的操作工具栏相关联。
当我运行它时,它会显示QCoreApplication::exec: The event loop is already running
。有人能告诉我错误和解决方案吗?先谢谢!
答案 0 :(得分:0)
从pyqt开始我遇到了类似的麻烦。 您应该为对话框窗口创建另一个类,如下所示:
#!python3
'''docs'''
import sys
from PyQt4.QtGui import *
from _apps.PyUi import DialogUi
class DialogApp(QDialog, DialogUi.Ui_DialogApp):
"""Dialog class"""
def __init__(self, parent=None):
super(self.__class__, self).__init__(parent)
self.setupUi(self)
def main(self):
app = QApplication(sys.argv)
form = DialogApp()
form.show()
app.exec_()
if __name__ == '__main__':
main()
包含的主要内容,便于测试...... 下一部分是在主窗口中,只是与操作相关的插槽:
def openDialog(self):
self.dialog= DialogApp(self)
self.dialog.show()
您需要注意的部分是谁是对话框的父级,如果None
是父级,就像在我的示例中一样,对话框将在任务栏上获得一个单独的选项卡,并且可以实例化为多个单击action
时的时间,假设对话框本身是无模式的。
如果将mainwindow
(self
)传递给对话框(在大多数情况下建议使用对话框窗口),则主要将是父对象,默认情况下,对话框在出现时将以main为中心。
此外,您需要并且只能激活一个QApplication
,根据我所知,显示的所有窗口以及基础方法和函数都会通过此初始QApplication
。