如何在PyQt中导入两个不同的gui文件?

时间:2015-12-13 21:09:34

标签: python user-interface pyqt4

首先抱歉我的英语不好。在我提出这个问题之前,我到处搜索过。我完全不明白。

我有两个gui文件。一个是主窗口,另一个是属性对话窗口。我有一个导入和调用gui文件的文件。我可以导入并调用主窗口。主窗口上有一个按钮(pbShowDiag)。我想在按下pbShowDiag按钮时调用属性对话框。我怎么能这样做?

调用脚本:

import sys
import openpyxl
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from main import *
from properties import Properties

class MyForm(QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        #Programı tam ekran çalıştır.
        self.setWindowState(Qt.WindowMaximized)

        #status bar iletilerini göster
        self.ui.statusbar.showMessage("The program has started")

        #önce excel dosyasını ardından excel sayfasını içe aktar.
        wb = openpyxl.load_workbook("librarything_yilmazao.xlsx")
        sheet = wb.get_sheet_by_name("librarything_yilmazao")
        #tabloya eklenecek verileri depola.
        data = []

        #excelin ilk satırındaki başlıkları qtablewidget a başlık olarak gönder.
        headerList = []
        for header in range(1,250):
            headerList.append(sheet.cell(row=1,column=header).value)
        self.ui.tableWidget.setHorizontalHeaderLabels(headerList)

        #depolanacak verileri çek.
        for row in range(2,250):
            rowData = []
            for col in range(1,44):
                rowData.append(sheet.cell(row=row,column=col).value)
            data.append(rowData)

        self.data = data
        self.addcontent()

        #Düğme işlevleri.
        QtCore.QObject.connect(self.ui.pbAddRow, QtCore.SIGNAL(("clicked()")), self.addRow)
        QtCore.QObject.connect(self.ui.pbShowDiag, QtCore.SIGNAL(("clicked()")), self.callProperties)


    #verileri widgeta gönder.
    def addcontent(self):
        row = 0
        for tup in self.data:
            col = 0
            for item in tup:
                anitem = QTableWidgetItem(item)
                self.ui.tableWidget.setItem(row,col,anitem)
                col += 1
            row += 1

    #Tabloya boş bir satır ekle.
    def addRow(self):
        rowPosition = self.ui.tableWidget.rowCount()
        self.ui.tableWidget.insertRow(rowPosition)
        self.ui.statusbar.showMessage("1 row added to the table")

    def callProperties(self):
        self.ozellikGui = Properties()
        self.ozellikGui.setupUi(self)




if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = MyForm()
    myapp.show()
    sys.exit(app.exec_())

主窗口:

    # -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'main.ui'
#
# Created: Sun Dec 13 20:29:28 2015
#      by: PyQt4 UI code generator 4.10.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_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(712, 560)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.pbAddRow = QtGui.QPushButton(self.centralwidget)
        self.pbAddRow.setGeometry(QtCore.QRect(10, 20, 94, 26))
        self.pbAddRow.setObjectName(_fromUtf8("pbAddRow"))
        self.pbRemove = QtGui.QPushButton(self.centralwidget)
        self.pbRemove.setGeometry(QtCore.QRect(10, 50, 94, 26))
        self.pbRemove.setObjectName(_fromUtf8("pbRemove"))
        self.pbShowDiag = QtGui.QPushButton(self.centralwidget)
        self.pbShowDiag.setGeometry(QtCore.QRect(10, 80, 94, 26))
        self.pbShowDiag.setObjectName(_fromUtf8("pbShowDiag"))
        self.tableWidget = QtGui.QTableWidget(self.centralwidget)
        self.tableWidget.setGeometry(QtCore.QRect(110, 20, 601, 491))
        self.tableWidget.setRowCount(248)
        self.tableWidget.setColumnCount(34)
        self.tableWidget.setObjectName(_fromUtf8("tableWidget"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 712, 23))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)


    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "Library", None))
        self.pbAddRow.setText(_translate("MainWindow", "Add Row", None))
        self.pbRemove.setText(_translate("MainWindow", "Remove", None))
        self.pbShowDiag.setText(_translate("MainWindow", "Show in Diag.", None))

属性对话框:

    # -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'properties.ui'
#
# Created: Sun Dec 13 12:30:16 2015
#      by: PyQt4 UI code generator 4.10.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 Properties(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(687, 539)
        self.graphicsView = QtGui.QGraphicsView(Dialog)
        self.graphicsView.setGeometry(QtCore.QRect(10, 60, 171, 241))
        self.graphicsView.setObjectName(_fromUtf8("graphicsView"))
        self.label = QtGui.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(20, 40, 56, 17))
        self.label.setObjectName(_fromUtf8("label"))
        self.label_2 = QtGui.QLabel(Dialog)
        self.label_2.setGeometry(QtCore.QRect(20, 10, 301, 17))
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.label_3 = QtGui.QLabel(Dialog)
        self.label_3.setGeometry(QtCore.QRect(200, 60, 101, 17))
        self.label_3.setObjectName(_fromUtf8("label_3"))
        self.label_4 = QtGui.QLabel(Dialog)
        self.label_4.setGeometry(QtCore.QRect(200, 90, 111, 17))
        self.label_4.setObjectName(_fromUtf8("label_4"))
        self.label_5 = QtGui.QLabel(Dialog)
        self.label_5.setGeometry(QtCore.QRect(200, 120, 91, 17))
        self.label_5.setObjectName(_fromUtf8("label_5"))
        self.label_6 = QtGui.QLabel(Dialog)
        self.label_6.setGeometry(QtCore.QRect(200, 150, 121, 17))
        self.label_6.setObjectName(_fromUtf8("label_6"))
        self.label_7 = QtGui.QLabel(Dialog)
        self.label_7.setGeometry(QtCore.QRect(200, 180, 56, 17))
        self.label_7.setObjectName(_fromUtf8("label_7"))
        self.label_8 = QtGui.QLabel(Dialog)
        self.label_8.setGeometry(QtCore.QRect(200, 210, 71, 17))
        self.label_8.setObjectName(_fromUtf8("label_8"))
        self.label_9 = QtGui.QLabel(Dialog)
        self.label_9.setGeometry(QtCore.QRect(200, 240, 131, 17))
        self.label_9.setObjectName(_fromUtf8("label_9"))
        self.label_10 = QtGui.QLabel(Dialog)
        self.label_10.setGeometry(QtCore.QRect(200, 270, 56, 17))
        self.label_10.setObjectName(_fromUtf8("label_10"))
        self.label_11 = QtGui.QLabel(Dialog)
        self.label_11.setGeometry(QtCore.QRect(20, 310, 56, 17))
        self.label_11.setObjectName(_fromUtf8("label_11"))
        self.label_12 = QtGui.QLabel(Dialog)
        self.label_12.setGeometry(QtCore.QRect(20, 340, 141, 17))
        self.label_12.setObjectName(_fromUtf8("label_12"))
        self.label_13 = QtGui.QLabel(Dialog)
        self.label_13.setGeometry(QtCore.QRect(20, 370, 56, 17))
        self.label_13.setObjectName(_fromUtf8("label_13"))
        self.label_14 = QtGui.QLabel(Dialog)
        self.label_14.setGeometry(QtCore.QRect(240, 310, 56, 17))
        self.label_14.setObjectName(_fromUtf8("label_14"))
        self.label_15 = QtGui.QLabel(Dialog)
        self.label_15.setGeometry(QtCore.QRect(240, 340, 56, 17))
        self.label_15.setObjectName(_fromUtf8("label_15"))
        self.label_16 = QtGui.QLabel(Dialog)
        self.label_16.setGeometry(QtCore.QRect(240, 370, 56, 17))
        self.label_16.setObjectName(_fromUtf8("label_16"))
        self.label_17 = QtGui.QLabel(Dialog)
        self.label_17.setGeometry(QtCore.QRect(240, 400, 56, 17))
        self.label_17.setObjectName(_fromUtf8("label_17"))
        self.label_18 = QtGui.QLabel(Dialog)
        self.label_18.setGeometry(QtCore.QRect(350, 310, 81, 17))
        self.label_18.setObjectName(_fromUtf8("label_18"))
        self.label_19 = QtGui.QLabel(Dialog)
        self.label_19.setGeometry(QtCore.QRect(470, 310, 81, 17))
        self.label_19.setObjectName(_fromUtf8("label_19"))
        self.label_20 = QtGui.QLabel(Dialog)
        self.label_20.setGeometry(QtCore.QRect(470, 340, 81, 17))
        self.label_20.setObjectName(_fromUtf8("label_20"))
        self.label_21 = QtGui.QLabel(Dialog)
        self.label_21.setGeometry(QtCore.QRect(470, 360, 56, 17))
        self.label_21.setObjectName(_fromUtf8("label_21"))
        self.label_22 = QtGui.QLabel(Dialog)
        self.label_22.setGeometry(QtCore.QRect(470, 390, 56, 17))
        self.label_22.setObjectName(_fromUtf8("label_22"))
        self.label_23 = QtGui.QLabel(Dialog)
        self.label_23.setGeometry(QtCore.QRect(590, 310, 56, 17))
        self.label_23.setObjectName(_fromUtf8("label_23"))
        self.label_24 = QtGui.QLabel(Dialog)
        self.label_24.setGeometry(QtCore.QRect(240, 430, 56, 17))
        self.label_24.setObjectName(_fromUtf8("label_24"))
        self.label_25 = QtGui.QLabel(Dialog)
        self.label_25.setGeometry(QtCore.QRect(240, 450, 56, 17))
        self.label_25.setObjectName(_fromUtf8("label_25"))
        self.label_26 = QtGui.QLabel(Dialog)
        self.label_26.setGeometry(QtCore.QRect(240, 470, 56, 17))
        self.label_26.setObjectName(_fromUtf8("label_26"))
        self.label_27 = QtGui.QLabel(Dialog)
        self.label_27.setGeometry(QtCore.QRect(470, 420, 111, 17))
        self.label_27.setObjectName(_fromUtf8("label_27"))
        self.label_28 = QtGui.QLabel(Dialog)
        self.label_28.setGeometry(QtCore.QRect(470, 450, 71, 17))
        self.label_28.setObjectName(_fromUtf8("label_28"))

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
        self.label.setText(_translate("Dialog", "Cover", None))
        self.label_2.setText(_translate("Dialog", "Book Title", None))
        self.label_3.setText(_translate("Dialog", "Author", None))
        self.label_4.setText(_translate("Dialog", "Other Authors", None))
        self.label_5.setText(_translate("Dialog", "Translator", None))
        self.label_6.setText(_translate("Dialog", "Publication", None))
        self.label_7.setText(_translate("Dialog", "Date", None))
        self.label_8.setText(_translate("Dialog", "Languages", None))
        self.label_9.setText(_translate("Dialog", "Original Languages", None))
        self.label_10.setText(_translate("Dialog", "Page", None))
        self.label_11.setText(_translate("Dialog", "Book ID", None))
        self.label_12.setText(_translate("Dialog", "First Publication Date", None))
        self.label_13.setText(_translate("Dialog", "Rating", None))
        self.label_14.setText(_translate("Dialog", "Height", None))
        self.label_15.setText(_translate("Dialog", "Weight", None))
        self.label_16.setText(_translate("Dialog", "Thickness", None))
        self.label_17.setText(_translate("Dialog", "Length", None))
        self.label_18.setText(_translate("Dialog", "Dimensions", None))
        self.label_19.setText(_translate("Dialog", "Date Started", None))
        self.label_20.setText(_translate("Dialog", "Date Read", None))
        self.label_21.setText(_translate("Dialog", "Barcode", None))
        self.label_22.setText(_translate("Dialog", "Tags", None))
        self.label_23.setText(_translate("Dialog", "ISBN", None))
        self.label_24.setText(_translate("Dialog", "Price", None))
        self.label_25.setText(_translate("Dialog", "Signed", None))
        self.label_26.setText(_translate("Dialog", "Copies", None))
        self.label_27.setText(_translate("Dialog", "Back Cover Text", None))
        self.label_28.setText(_translate("Dialog", "Comment", None))

1 个答案:

答案 0 :(得分:0)

首先,您应该为属性对话框创建一个新类,就像您对MyForm的类Ui_MainWindow所做的那样。您可以在同一个文件中执行此操作。通过这种方式,您可以轻松定义方法,使用插槽连接信号,......

class MyProperty(QDialog):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.ui = Properties()
        self.ui.setupUi(self)

        # connects, intialisations, ...

然后你可以用:

来调用它
def callProperties(self):
    properties_dialog = MyProperty()
    properties_dialog.exec_() # modal dialog

def callProperties(self):
    properties_dialog = MyProperty()
    properties_dialog.show() # non-modal dialog

模态对话框表示您可以单击主窗口中的任何内容,直到关闭属性对话框。