从PyQt5中的MenuItem打开新的对话框窗口

时间:2019-05-25 20:09:11

标签: python python-3.x pyqt pyqt5

我想在单击时从MenuObject生成一个新的对话框窗口。这是我的代码;简要地讲,我创建了一个名为submenu的类,该类采用QDialog,并在由open_submenu触发时创建了一个名为self.actionAyarlar.triggered.connect(self.open_submenu)的方法。基本上,当我按下“ Ayarlar”菜单项时,需要打开一个新的“对话”窗口。但这不是吗?

修改后的代码:

from PyQt5 import QtCore, QtGui, QtWidgets
import sys


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(873, 663)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
        self.textEdit.setGeometry(QtCore.QRect(110, 80, 71, 41))
        self.textEdit.setObjectName("textEdit")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(130, 160, 55, 16))
        self.label.setText("")
        self.label.setObjectName("label")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(220, 90, 93, 28))
        self.pushButton.setObjectName("pushButton")
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(120, 200, 113, 22))
        self.lineEdit.setObjectName("lineEdit")
        self.listWidget = QtWidgets.QListWidget(self.centralwidget)
        self.listWidget.setGeometry(QtCore.QRect(150, 330, 256, 192))
        font = QtGui.QFont()
        font.setFamily("Agency FB")
        self.listWidget.setFont(font)
        self.listWidget.setObjectName("listWidget")
        item = QtWidgets.QListWidgetItem()
        font = QtGui.QFont()
        font.setFamily("Algerian")
        item.setFont(font)
        self.listWidget.addItem(item)
        item = QtWidgets.QListWidgetItem()
        self.listWidget.addItem(item)
        self.plainTextEdit = QtWidgets.QPlainTextEdit(self.centralwidget)
        self.plainTextEdit.setGeometry(QtCore.QRect(440, 200, 104, 87))
        self.plainTextEdit.setObjectName("plainTextEdit")
        self.comboBox = QtWidgets.QComboBox(self.centralwidget)
        self.comboBox.setGeometry(QtCore.QRect(560, 380, 73, 22))
        self.comboBox.setObjectName("comboBox")
        self.comboBox.addItem("")
        self.comboBox.addItem("")
        self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox.setGeometry(QtCore.QRect(400, 70, 120, 80))
        self.groupBox.setObjectName("groupBox")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(610, 190, 55, 16))
        self.label_2.setObjectName("label_2")
        self.spinBox = QtWidgets.QSpinBox(self.centralwidget)
        self.spinBox.setGeometry(QtCore.QRect(70, 320, 42, 22))
        self.spinBox.setProperty("value", 0)
        self.spinBox.setObjectName("spinBox")
        self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_2.setGeometry(QtCore.QRect(10, 370, 113, 22))
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.fontComboBox = QtWidgets.QFontComboBox(self.centralwidget)
        self.fontComboBox.setGeometry(QtCore.QRect(530, 480, 226, 22))
        self.fontComboBox.setObjectName("fontComboBox")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 873, 26))
        self.menubar.setObjectName("menubar")
        self.menuDosya = QtWidgets.QMenu(self.menubar)
        self.menuDosya.setObjectName("menuDosya")

        self.menuD_zenle = QtWidgets.QMenu(self.menubar)
        self.menuD_zenle.setObjectName("menuD_zenle")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.actionAyarlar = QtWidgets.QAction(MainWindow)
        self.actionAyarlar.setObjectName("actionAyarlar")
        self.actionUndo = QtWidgets.QAction(MainWindow)
        self.actionUndo.setObjectName("actionUndo")
        self.menuD_zenle.addAction(self.actionAyarlar)

        self.actionAyarlar.triggered.connect(self.open_submenu)

        self.menuD_zenle.addAction(self.actionUndo)
        self.menubar.addAction(self.menuDosya.menuAction())
        self.menubar.addAction(self.menuD_zenle.menuAction())

        self.retranslateUi(MainWindow)

        self.textEdit.textChanged.connect(self.convert)

        self.lineEdit.textEdited['QString'].connect(self.label.setText)
        self.pushButton.pressed.connect(self.plainTextEdit.clear)
        self.pushButton.pressed.connect(self.listWidget.clear)
        self.comboBox.activated['QString'].connect(self.lineEdit.setText)
        self.spinBox.valueChanged['QString'].connect(self.lineEdit_2.setText)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
    def convert(self):
        some_text=self.textEdit.toPlainText()
        value=int(some_text)**2
        self.lineEdit.setText("%s"%value)


    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.textEdit.setHtml(_translate("MainWindow", "<!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:\'MS Shell Dlg 2\'; font-size:7.8pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">g</p></body></html>"))
        self.pushButton.setText(_translate("MainWindow", "PushButton"))
        self.lineEdit.setText(_translate("MainWindow", "k"))
        __sortingEnabled = self.listWidget.isSortingEnabled()
        self.listWidget.setSortingEnabled(False)
        item = self.listWidget.item(0)
        item.setText(_translate("MainWindow", "aaszd"))
        item = self.listWidget.item(1)
        item.setText(_translate("MainWindow", "aaa"))
        self.listWidget.setSortingEnabled(__sortingEnabled)
        self.comboBox.setItemText(0, _translate("MainWindow", "montecarlo"))
        self.comboBox.setItemText(1, _translate("MainWindow", "normal"))
        self.groupBox.setTitle(_translate("MainWindow", "GroupBox"))
        self.label_2.setText(_translate("MainWindow", "TextLabel"))
        self.spinBox.setToolTip(_translate("MainWindow", "<html><head/><body><p></p></body></html>"))
        self.menuDosya.setTitle(_translate("MainWindow", "Dosya"))
        self.menuD_zenle.setTitle(_translate("MainWindow", "Düzenle"))
        self.actionAyarlar.setText(_translate("MainWindow", "Ayarlar"))
        self.actionUndo.setText(_translate("MainWindow", "Undo"))
    def open_submenu(self):
        sub_screen=QtWidgets.QApplication(sys.argv)
        DialogWindow=QtWidgets.QDialog()
        submenu=submenu()
        submenu.setupUi(DialogWindow)
        DialogWindow.show()
        sys.exit(DialogWindow.exec_())


class submenu(object):
    def setupUi(DialogWindow):
        DialogWindow.setObjectName("DialogWindow")
        DialogWindow.resize(400, 300)
        self.retranslateUi(DialogWindow)
        QtCore.QMetaObject.connectSlotsByName(DialogWindow) 
    def retranslateUi(self, DialogWindow):
        _translate = QtCore.QCoreApplication.translate
        DialogWindow.setWindowTitle(_translate("DialogWindow", "DialogWindow"))






if __name__ == "__main__":

#    fig1 = Figure()
    if not QtWidgets.QApplication.instance():
        app = QtWidgets.QApplication(sys.argv)
    else:
        app = QtWidgets.QApplication.instance() 
#    main = Main()
#    main.addmpl(fig1)


    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())       

0 个答案:

没有答案