将多个.ui文件导入main.py文件

时间:2014-08-19 12:35:32

标签: python database user-interface pyqt

我设法将我的main_GUI文件导入到我的main.py文件中,但我想要实现另一个GUI,该GUI会显示单击按钮的结果main_GUI文件。这可能吗?

这是我的主文件(bazapodataka.py),其中所有其他.ui文件都将被调用

import sys
from PyQt4.QtSql import *
from PyQt4 import QtCore, QtGui, uic 

class TestApp(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)

        self.ui = uic.loadUi("C:\Python34\Lib\site-packages\PyQt4\BazaPodataka\database_GUI.ui")
        self.ui.show()  


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    win = TestApp()

db = QSqlDatabase.addDatabase("QPSQL")
db.setHostName("localhost")
db.setDatabaseName("BP Za bolnicu")
db.setUserName("postgres")
db.setPassword("zanoktica")
db.setPort(5432)
ok = db.open()

if ok:
    print("Connected to database")
else:
    print("Error connecting to database")

sys.exit(app.exec_())

这是我的main_GUI文件(database_GUI.ui)

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_Main_GUI_Widget(object):
    def setupUi(self, Main_GUI_Widget):
        Main_GUI_Widget.setObjectName(_fromUtf8("Main_GUI_Widget"))
        Main_GUI_Widget.resize(300, 228)
        self.horizontalLayout_5 = QtGui.QHBoxLayout(Main_GUI_Widget)
        self.horizontalLayout_5.setObjectName(_fromUtf8("horizontalLayout_5"))
        self.verticalLayout = QtGui.QVBoxLayout()
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
        self.horizontalLayout = QtGui.QHBoxLayout()
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
        self.Add_button = QtGui.QPushButton(Main_GUI_Widget)
        self.Add_button.setObjectName(_fromUtf8("Add_button"))
        self.horizontalLayout.addWidget(self.Add_button)
        self.Add_comboBox = QtGui.QComboBox(Main_GUI_Widget)
        self.Add_comboBox.setObjectName(_fromUtf8("Add_comboBox"))
        self.Add_comboBox.addItem(_fromUtf8(""))
        self.Add_comboBox.addItem(_fromUtf8(""))
        self.Add_comboBox.addItem(_fromUtf8(""))
        self.Add_comboBox.addItem(_fromUtf8(""))
        self.Add_comboBox.addItem(_fromUtf8(""))
        self.Add_comboBox.addItem(_fromUtf8(""))
        self.Add_comboBox.addItem(_fromUtf8(""))
        self.Add_comboBox.addItem(_fromUtf8(""))
        self.Add_comboBox.addItem(_fromUtf8(""))
        self.Add_comboBox.addItem(_fromUtf8(""))
        self.Add_comboBox.addItem(_fromUtf8(""))
        self.Add_comboBox.addItem(_fromUtf8(""))
        self.horizontalLayout.addWidget(self.Add_comboBox)
        self.verticalLayout.addLayout(self.horizontalLayout)
        self.horizontalLayout_2 = QtGui.QHBoxLayout()
        self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2"))
        self.Delete_button = QtGui.QPushButton(Main_GUI_Widget)
        self.Delete_button.setObjectName(_fromUtf8("Delete_button"))
        self.horizontalLayout_2.addWidget(self.Delete_button)
        self.Delete_comboBox = QtGui.QComboBox(Main_GUI_Widget)
        self.Delete_comboBox.setObjectName(_fromUtf8("Delete_comboBox"))
        self.Delete_comboBox.addItem(_fromUtf8(""))
        self.Delete_comboBox.addItem(_fromUtf8(""))
        self.Delete_comboBox.addItem(_fromUtf8(""))
        self.Delete_comboBox.addItem(_fromUtf8(""))
        self.Delete_comboBox.addItem(_fromUtf8(""))
        self.Delete_comboBox.addItem(_fromUtf8(""))
        self.Delete_comboBox.addItem(_fromUtf8(""))
        self.Delete_comboBox.addItem(_fromUtf8(""))
        self.Delete_comboBox.addItem(_fromUtf8(""))
        self.Delete_comboBox.addItem(_fromUtf8(""))
        self.Delete_comboBox.addItem(_fromUtf8(""))
        self.Delete_comboBox.addItem(_fromUtf8(""))
        self.horizontalLayout_2.addWidget(self.Delete_comboBox)
        self.verticalLayout.addLayout(self.horizontalLayout_2)
        self.horizontalLayout_3 = QtGui.QHBoxLayout()
        self.horizontalLayout_3.setObjectName(_fromUtf8("horizontalLayout_3"))
        self.Update_button = QtGui.QPushButton(Main_GUI_Widget)
        self.Update_button.setObjectName(_fromUtf8("Update_button"))
        self.horizontalLayout_3.addWidget(self.Update_button)
        self.Update_comboBox = QtGui.QComboBox(Main_GUI_Widget)
        self.Update_comboBox.setObjectName(_fromUtf8("Update_comboBox"))
        self.Update_comboBox.addItem(_fromUtf8(""))
        self.Update_comboBox.addItem(_fromUtf8(""))
        self.Update_comboBox.addItem(_fromUtf8(""))
        self.Update_comboBox.addItem(_fromUtf8(""))
        self.Update_comboBox.addItem(_fromUtf8(""))
        self.Update_comboBox.addItem(_fromUtf8(""))
        self.Update_comboBox.addItem(_fromUtf8(""))
        self.Update_comboBox.addItem(_fromUtf8(""))
        self.Update_comboBox.addItem(_fromUtf8(""))
        self.Update_comboBox.addItem(_fromUtf8(""))
        self.Update_comboBox.addItem(_fromUtf8(""))
        self.Update_comboBox.addItem(_fromUtf8(""))
        self.horizontalLayout_3.addWidget(self.Update_comboBox)
        self.verticalLayout.addLayout(self.horizontalLayout_3)
        self.horizontalLayout_4 = QtGui.QHBoxLayout()
        self.horizontalLayout_4.setObjectName(_fromUtf8("horizontalLayout_4"))
        self.Search_button = QtGui.QPushButton(Main_GUI_Widget)
        self.Search_button.setObjectName(_fromUtf8("Search_button"))
        self.horizontalLayout_4.addWidget(self.Search_button)
        self.Search_comboBox = QtGui.QComboBox(Main_GUI_Widget)
        self.Search_comboBox.setObjectName(_fromUtf8("Search_comboBox"))
        self.Search_comboBox.addItem(_fromUtf8(""))
        self.Search_comboBox.addItem(_fromUtf8(""))
        self.Search_comboBox.addItem(_fromUtf8(""))
        self.Search_comboBox.addItem(_fromUtf8(""))
        self.Search_comboBox.addItem(_fromUtf8(""))
        self.Search_comboBox.addItem(_fromUtf8(""))
        self.Search_comboBox.addItem(_fromUtf8(""))
        self.Search_comboBox.addItem(_fromUtf8(""))
        self.Search_comboBox.addItem(_fromUtf8(""))
        self.Search_comboBox.addItem(_fromUtf8(""))
        self.Search_comboBox.addItem(_fromUtf8(""))
        self.Search_comboBox.addItem(_fromUtf8(""))
        self.horizontalLayout_4.addWidget(self.Search_comboBox)
        self.verticalLayout.addLayout(self.horizontalLayout_4)
        self.horizontalLayout_5.addLayout(self.verticalLayout)

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

    def retranslateUi(self, Main_GUI_Widget):
        Main_GUI_Widget.setWindowTitle(_translate("Main_GUI_Widget", "Form", None))
        self.Add_button.setText(_translate("Main_GUI_Widget", "Add", None))
        self.Add_comboBox.setItemText(0, _translate("Main_GUI_Widget", "asistira", None))
        self.Add_comboBox.setItemText(1, _translate("Main_GUI_Widget", "kirurg", None))
        self.Add_comboBox.setItemText(2, _translate("Main_GUI_Widget", "konzultant", None))
        self.Add_comboBox.setItemText(3, _translate("Main_GUI_Widget", "nadgleda", None))
        self.Add_comboBox.setItemText(4, _translate("Main_GUI_Widget", "operacija", None))
        self.Add_comboBox.setItemText(5, _translate("Main_GUI_Widget", "pacijent", None))
        self.Add_comboBox.setItemText(6, _translate("Main_GUI_Widget", "privatni_pacijent", None))
        self.Add_comboBox.setItemText(7, _translate("Main_GUI_Widget", "sala", None))
        self.Add_comboBox.setItemText(8, _translate("Main_GUI_Widget", "sestra", None))
        self.Add_comboBox.setItemText(9, _translate("Main_GUI_Widget", "soba", None))
        self.Add_comboBox.setItemText(10, _translate("Main_GUI_Widget", "zaduzena_za_salu", None))
        self.Add_comboBox.setItemText(11, _translate("Main_GUI_Widget", "zaduzena_za_sobu", None))
        self.Delete_button.setText(_translate("Main_GUI_Widget", "Delete", None))
        self.Delete_comboBox.setItemText(0, _translate("Main_GUI_Widget", "asistira", None))
        self.Delete_comboBox.setItemText(1, _translate("Main_GUI_Widget", "kirurg", None))
        self.Delete_comboBox.setItemText(2, _translate("Main_GUI_Widget", "konzultant", None))
        self.Delete_comboBox.setItemText(3, _translate("Main_GUI_Widget", "nadgleda", None))
        self.Delete_comboBox.setItemText(4, _translate("Main_GUI_Widget", "operacija", None))
        self.Delete_comboBox.setItemText(5, _translate("Main_GUI_Widget", "pacijent", None))
        self.Delete_comboBox.setItemText(6, _translate("Main_GUI_Widget", "privatni_pacijent", None))
        self.Delete_comboBox.setItemText(7, _translate("Main_GUI_Widget", "sala", None))
        self.Delete_comboBox.setItemText(8, _translate("Main_GUI_Widget", "sestra", None))
        self.Delete_comboBox.setItemText(9, _translate("Main_GUI_Widget", "soba", None))
        self.Delete_comboBox.setItemText(10, _translate("Main_GUI_Widget", "zaduzena_za_salu", None))
        self.Delete_comboBox.setItemText(11, _translate("Main_GUI_Widget", "zaduzena_za_sobu", None))
        self.Update_button.setText(_translate("Main_GUI_Widget", "Update", None))
        self.Update_comboBox.setItemText(0, _translate("Main_GUI_Widget", "asistira", None))
        self.Update_comboBox.setItemText(1, _translate("Main_GUI_Widget", "kirurg", None))
        self.Update_comboBox.setItemText(2, _translate("Main_GUI_Widget", "konzultant", None))
        self.Update_comboBox.setItemText(3, _translate("Main_GUI_Widget", "nadgleda", None))
        self.Update_comboBox.setItemText(4, _translate("Main_GUI_Widget", "operacija", None))
        self.Update_comboBox.setItemText(5, _translate("Main_GUI_Widget", "pacijent", None))
        self.Update_comboBox.setItemText(6, _translate("Main_GUI_Widget", "privatni_pacijent", None))
        self.Update_comboBox.setItemText(7, _translate("Main_GUI_Widget", "sala", None))
        self.Update_comboBox.setItemText(8, _translate("Main_GUI_Widget", "sestra", None))
        self.Update_comboBox.setItemText(9, _translate("Main_GUI_Widget", "soba", None))
        self.Update_comboBox.setItemText(10, _translate("Main_GUI_Widget", "zaduzena_za_salu", None))
        self.Update_comboBox.setItemText(11, _translate("Main_GUI_Widget", "zaduzena_za_sobu", None))
        self.Search_button.setText(_translate("Main_GUI_Widget", "Search", None))
        self.Search_comboBox.setItemText(0, _translate("Main_GUI_Widget", "asistira", None))
        self.Search_comboBox.setItemText(1, _translate("Main_GUI_Widget", "kirurg", None))
        self.Search_comboBox.setItemText(2, _translate("Main_GUI_Widget", "konzultant", None))
        self.Search_comboBox.setItemText(3, _translate("Main_GUI_Widget", "nadgleda", None))
        self.Search_comboBox.setItemText(4, _translate("Main_GUI_Widget", "operacija", None))
        self.Search_comboBox.setItemText(5, _translate("Main_GUI_Widget", "pacijent", None))
        self.Search_comboBox.setItemText(6, _translate("Main_GUI_Widget", "privatni_pacijent", None))
        self.Search_comboBox.setItemText(7, _translate("Main_GUI_Widget", "sala", None))
        self.Search_comboBox.setItemText(8, _translate("Main_GUI_Widget", "sestra", None))
        self.Search_comboBox.setItemText(9, _translate("Main_GUI_Widget", "soba", None))
        self.Search_comboBox.setItemText(10, _translate("Main_GUI_Widget", "zaduzena_za_salu", None))
        self.Search_comboBox.setItemText(11, _translate("Main_GUI_Widget", "zaduzena_za_sobu", None))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Main_GUI_Widget = QtGui.QWidget()
    ui = Ui_Main_GUI_Widget()
    ui.setupUi(Main_GUI_Widget)
    Main_GUI_Widget.show()
    sys.exit(app.exec_())

和我的最后一个gui文件(kirurg_GUI.ui)

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_kirurg_Widget(object):
    def setupUi(self, kirurg_Widget):
        kirurg_Widget.setObjectName(_fromUtf8("kirurg_Widget"))
        kirurg_Widget.resize(437, 301)
        self.horizontalLayout_6 = QtGui.QHBoxLayout(kirurg_Widget)
        self.horizontalLayout_6.setObjectName(_fromUtf8("horizontalLayout_6"))
        self.verticalLayout_2 = QtGui.QVBoxLayout()
        self.verticalLayout_2.setObjectName(_fromUtf8("verticalLayout_2"))
        self.verticalLayout = QtGui.QVBoxLayout()
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
        self.horizontalLayout = QtGui.QHBoxLayout()
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
        self.oib_kir_label = QtGui.QLabel(kirurg_Widget)
        self.oib_kir_label.setObjectName(_fromUtf8("oib_kir_label"))
        self.horizontalLayout.addWidget(self.oib_kir_label)
        self.oib_kir_lineEdit = QtGui.QLineEdit(kirurg_Widget)
        self.oib_kir_lineEdit.setObjectName(_fromUtf8("oib_kir_lineEdit"))
        self.horizontalLayout.addWidget(self.oib_kir_lineEdit)
        self.verticalLayout.addLayout(self.horizontalLayout)
        self.horizontalLayout_2 = QtGui.QHBoxLayout()
        self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2"))
        self.prezime_label = QtGui.QLabel(kirurg_Widget)
        self.prezime_label.setObjectName(_fromUtf8("prezime_label"))
        self.horizontalLayout_2.addWidget(self.prezime_label)
        self.prezime_lineEdit = QtGui.QLineEdit(kirurg_Widget)
        self.prezime_lineEdit.setObjectName(_fromUtf8("prezime_lineEdit"))
        self.horizontalLayout_2.addWidget(self.prezime_lineEdit)
        self.verticalLayout.addLayout(self.horizontalLayout_2)
        self.horizontalLayout_3 = QtGui.QHBoxLayout()
        self.horizontalLayout_3.setObjectName(_fromUtf8("horizontalLayout_3"))
        self.ime_label = QtGui.QLabel(kirurg_Widget)
        self.ime_label.setObjectName(_fromUtf8("ime_label"))
        self.horizontalLayout_3.addWidget(self.ime_label)
        self.ime_lineEdit = QtGui.QLineEdit(kirurg_Widget)
        self.ime_lineEdit.setObjectName(_fromUtf8("ime_lineEdit"))
        self.horizontalLayout_3.addWidget(self.ime_lineEdit)
        self.verticalLayout.addLayout(self.horizontalLayout_3)
        self.horizontalLayout_4 = QtGui.QHBoxLayout()
        self.horizontalLayout_4.setObjectName(_fromUtf8("horizontalLayout_4"))
        self.adresa_label = QtGui.QLabel(kirurg_Widget)
        self.adresa_label.setObjectName(_fromUtf8("adresa_label"))
        self.horizontalLayout_4.addWidget(self.adresa_label)
        self.adresa_lineEdit = QtGui.QLineEdit(kirurg_Widget)
        self.adresa_lineEdit.setObjectName(_fromUtf8("adresa_lineEdit"))
        self.horizontalLayout_4.addWidget(self.adresa_lineEdit)
        self.verticalLayout.addLayout(self.horizontalLayout_4)
        self.horizontalLayout_5 = QtGui.QHBoxLayout()
        self.horizontalLayout_5.setObjectName(_fromUtf8("horizontalLayout_5"))
        self.broj_telefona_label = QtGui.QLabel(kirurg_Widget)
        self.broj_telefona_label.setObjectName(_fromUtf8("broj_telefona_label"))
        self.horizontalLayout_5.addWidget(self.broj_telefona_label)
        self.broj_telefona_lineEdit = QtGui.QLineEdit(kirurg_Widget)
        self.broj_telefona_lineEdit.setObjectName(_fromUtf8("broj_telefona_lineEdit"))
        self.horizontalLayout_5.addWidget(self.broj_telefona_lineEdit)
        self.verticalLayout.addLayout(self.horizontalLayout_5)
        self.verticalLayout_2.addLayout(self.verticalLayout)
        spacerItem = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
        self.verticalLayout_2.addItem(spacerItem)
        self.kirurg_Add_button = QtGui.QPushButton(kirurg_Widget)
        self.kirurg_Add_button.setObjectName(_fromUtf8("kirurg_Add_button"))
        self.verticalLayout_2.addWidget(self.kirurg_Add_button)
        spacerItem1 = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
        self.verticalLayout_2.addItem(spacerItem1)
        self.horizontalLayout_6.addLayout(self.verticalLayout_2)

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

    def retranslateUi(self, kirurg_Widget):
        kirurg_Widget.setWindowTitle(_translate("kirurg_Widget", "Form", None))
        self.oib_kir_label.setText(_translate("kirurg_Widget", "oib_kir (character(11) NOT NULL)", None))
        self.prezime_label.setText(_translate("kirurg_Widget", "prezime (text)", None))
        self.ime_label.setText(_translate("kirurg_Widget", "ime (text)", None))
        self.adresa_label.setText(_translate("kirurg_Widget", "adresa (text)", None))
        self.broj_telefona_label.setText(_translate("kirurg_Widget", "broj_telefona (varchar(12))", None))
        self.kirurg_Add_button.setText(_translate("kirurg_Widget", "Add", None))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    kirurg_Widget = QtGui.QWidget()
    ui = Ui_kirurg_Widget()
    ui.setupUi(kirurg_Widget)
    kirurg_Widget.show()
    sys.exit(app.exec_())

基本上,我设法将database_GUI.ui文件实现到我的bazapodataka.py文件中,但现在我想打开我的第二个GUI文件(kirurg_GUI.ui),我从"kirurg"选择了comboBox选项,然后点击Add。那可能吗?

编辑:在bazapodataka.py中导入时,我的database_GUI.ui类是否会转发所有按钮,组合框等?

另外,这是最好的方法吗?我的意思是,将外部GUI实现到一个主要的python文件?

编辑2:

import sys
from PyQt4.QtSql import *
from PyQt4 import QtCore, QtGui, uic
from database_GUI import Ui_Main_GUI_Widget
from kirurg_GUI import Ui_kirurg_Widget

class database_GUI(QtGui.QWidget, Ui_Main_GUI_Widget):
       def __init__(self):
              QtGui.QWidget.__init__(self)
              self.setupUi(self)

              button = QtGui.QPushButton("Add_button")
              button.clicked.connect(self.open_kirurg_ui)

       def open_kirurg_ui(self):
              kirurg = KirurgUi()
              kirurg.show()

class kirurg_GUI(QtGui.QWidget, Ui_kirurg_Widget):
        def __init__(self):
              QtGui.QWidget.__init__(self)
              self.setupUi(self)

if __name__ == "__main__":
       app = QtGui.QApplication(sys.argv)
       main_GUI = database_GUI()
       main_GUI.show()

db = QSqlDatabase.addDatabase("QPSQL")
db.setHostName("localhost")
db.setDatabaseName("BP Za bolnicu")
db.setUserName("postgres")
db.setPassword("zanoktica")
db.setPort(5432)
ok = db.open()

if ok:
    print("Connected to database")
else:
    print("Error connecting to database")

sys.exit(app.exec_())

出于某种原因,它不会连接到我的数据库,除非在此之后放入该数据库代码:

if __name__ == "__main__":
           app = QtGui.QApplication(sys.argv)
           main_GUI = database_GUI()
           main_GUI.show()

1 个答案:

答案 0 :(得分:1)

是的,当然有可能,但我会考虑进行一些重构。

假设您有三个文件:

  • bazapodataka.py - 这是将加载所有UI的主要python文件
  • database_ui.py - 这是您的第一个用户界面
  • kirurg_ui.py - 在第一个用户界面中点击按钮时调用的用户界面

您的bazapodataka.py应该是这样的:

import sys
from PyQt4.QtSql import *
from PyQt4 import QtCore, QtGui, uic
from database_ui import Ui_Main_Gui_Widget #importing UI as py files generated by pyuic
from kirurg_ui import Ui_kirurg_Widget

class TestApp(QtGui.QWidget, Ui_Main_Gui_Widget): #inherit from Ui_Main_Gui_Widget
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.setupUi() #call the Ui_Main_Gui_Widget's setupUi method

        self.kirurg_Add_button.clicked.connect(self.open_kirurg_ui)

    def open_kirurg_ui(self):
        if self.Add_comboBox.currentText() == "kirurg":
            kirurg = KirurgUi()
            kirurg.exec_()


class KirurgUi(QtGui.QWidget, Ui_kirurg_Widget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.setupUi()


db = QSqlDatabase.addDatabase("QPSQL")
db.setHostName("localhost")
db.setDatabaseName("BP Za bolnicu")
db.setUserName("postgres")
db.setPassword("zanoktica")
db.setPort(5432)
ok = db.open()
if ok:
    print("Connected to database")
else:
    print("Error connecting to database")

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

注意:我没有测试应用程序,但应该可以使用。

Pozdrav