如何从已经打开的另一个窗口打开窗口?

时间:2018-10-14 13:49:46

标签: python pyqt pyqt5

我有一个文件,其中包含2个定义UI的类:主要的一个和次要的。我有从main_GUI.py导入的主程序:

from main_GUI import Ui_Dodaj_Ksiazke
from main_GUI import Ui_dodaj_autora

并显示主窗口:

class Main(QtWidgets.QMainWindow,Ui_Dodaj_Ksiazke):
    def __init__(self):
       QtWidgets.QMainWindow.__init__(self)
       self.ui = Ui_Dodaj_Ksiazke()
       self.ui.setupUi(self)

在程序的某个时刻,我定义了函数,如果用户从列表中选择某个选项,它将打开新窗口(GUI.py中的第二类):

def autorchange(self):

    item = self.ui.autor.currentText()
    nazwisko, imie = item.split()
    if item == "Dodaj autora...":
        #this is where program is supposed to show second window for user to interact with

如何显示此窗口,以便可以对其执行其他操作?

此新窗口具有两个文本字段,供用户编写值,接下来应将其插入SQL数据库,然后窗口应关闭。

我尝试使用,与主窗口相同,但失败并出现错误:

QLayout: Attempting to add QLayout "" to Main "dodaj_autora", which already has a layout Traceback (most recent call last):   File "C:\Users\jakub\PycharmProjects\biblioteka\main.py", line 46, in autorchange
    self.ui.setupUi(self)   File "C:\Users\jakub\PycharmProjects\biblioteka\main_GUI.py", line 231, in setupUi
    self.buttonBox.accepted.connect(dodaj_autora.accept) AttributeError: 'Main' object has no attribute 'accept'

我也尝试了不同的方法。我创建了新课程:

class Autor(QtWidgets.QMainWindow,Ui_dodaj_autora):
    def __init__(self):
       QtWidgets.QMainWindow.__init__(self)
       self.ui = Ui_dodaj_autora()
       self.ui.setupUi(self)

然后在Main类和函数定义中检查用户从列表中选择了哪个选项:

def autorchange(self):

    item = self.ui.autor.currentText()
    nazwisko, imie = item.split()
    if item == "Dodaj autora...":
        print("Dodawanie autora")
        okno = Autor()
        okno.show()
return item

但这也会导致错误:

QLayout: Attempting to add QLayout "" to Autor "dodaj_autora", which already has a layout

我已经准备了运行该程序所需的最少代码。在这里能找到它: debug.py

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from debug_GUI import Ui_Dodaj_Ksiazke
from debug_GUI import Ui_dodaj_autora


class Autor(QtWidgets.QMainWindow,Ui_dodaj_autora):
    def __init__(self):
       QtWidgets.QMainWindow.__init__(self)
       self.ui = Ui_dodaj_autora()
       self.ui.setupUi(self)

class Main(QtWidgets.QMainWindow,Ui_Dodaj_Ksiazke):
    def __init__(self):
       QtWidgets.QMainWindow.__init__(self)
       self.ui = Ui_Dodaj_Ksiazke()
       self.ui.setupUi(self)

    def pole_autor(self):

        self.ui.autor.addItem("Wybierz autora...")
        self.ui.autor.addItem("Dodaj autora...")

        self.ui.autor.currentIndexChanged.connect(self.autorchange)

    def autorchange(self):

        item = self.ui.autor.currentText()

        if item == "Dodaj autora...":
            print("Dodawanie autora")
            okno = Autor()
            okno.show()
        return item

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = Main()
    window.pole_autor()
    window.show()
    sys.exit(app.exec_())

和debug_GUI.py

from PyQt5 import QtCore, QtWidgets

class Ui_Dodaj_Ksiazke(object):
    def setupUi(self, Dodaj_Ksiazke):
        Dodaj_Ksiazke.setObjectName("Dodaj_Ksiazke")
        Dodaj_Ksiazke.resize(450, 600)
        Dodaj_Ksiazke.setMaximumSize(QtCore.QSize(450, 600))
        self.centralwidget = QtWidgets.QWidget(Dodaj_Ksiazke)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.gridLayout_2 = QtWidgets.QGridLayout()
        self.gridLayout_2.setObjectName("gridLayout_2")

        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setObjectName("label")
        self.gridLayout_2.addWidget(self.label, 3, 0, 1, 1)

        self.autor = QtWidgets.QComboBox(self.centralwidget)
        self.autor.setObjectName("autor")
        self.gridLayout_2.addWidget(self.autor, 3, 2, 1, 1)

        Dodaj_Ksiazke.setCentralWidget(self.centralwidget)

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

    def retranslateUi(self, Dodaj_Ksiazke):
        _translate = QtCore.QCoreApplication.translate
        Dodaj_Ksiazke.setWindowTitle(_translate("Dodaj_Ksiazke", "Dodaj Książkę"))
        self.label.setText(_translate("Dodaj_Ksiazke", "Autor"))

class Ui_dodaj_autora(object):
    def setupUi(self, dodaj_autora):
        dodaj_autora.setObjectName("dodaj_autora")
        dodaj_autora.resize(400, 100)
        dodaj_autora.setMaximumSize(QtCore.QSize(400, 100))
        self.verticalLayout = QtWidgets.QVBoxLayout(dodaj_autora)
        self.verticalLayout.setObjectName("verticalLayout")
        self.gridLayout = QtWidgets.QGridLayout()
        self.gridLayout.setObjectName("gridLayout")
        self.label_2 = QtWidgets.QLabel(dodaj_autora)
        self.label_2.setObjectName("label_2")
        self.gridLayout.addWidget(self.label_2, 1, 0, 1, 1)
        self.label = QtWidgets.QLabel(dodaj_autora)
        self.label.setObjectName("label")
        self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
        self.lineEdit = QtWidgets.QLineEdit(dodaj_autora)
        self.lineEdit.setObjectName("lineEdit")
        self.gridLayout.addWidget(self.lineEdit, 0, 1, 1, 1)
        self.lineEdit_2 = QtWidgets.QLineEdit(dodaj_autora)
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.gridLayout.addWidget(self.lineEdit_2, 1, 1, 1, 1)
        self.verticalLayout.addLayout(self.gridLayout)
        self.buttonBox = QtWidgets.QDialogButtonBox(dodaj_autora)
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
        self.buttonBox.setObjectName("buttonBox")
        self.verticalLayout.addWidget(self.buttonBox)

        self.retranslateUi(dodaj_autora)

    def retranslateUi(self, dodaj_autora):
        _translate = QtCore.QCoreApplication.translate
        dodaj_autora.setWindowTitle(_translate("dodaj_autora", "Dodaj Autora"))
        self.label_2.setText(_translate("dodaj_autora", "Nazwisko"))
        self.label.setText(_translate("dodaj_autora", "Imię"))

您需要单击下拉列表,然后选择“ Dodaj autora ...”。这应该打开在debug_GUI.py中定义的新窗口-这是类“ Ui_dodaj_autora”

https://bitbucket.org/snippets/hyperqbe/rezEBa-GUI https://bitbucket.org/snippets/hyperqbe/Ee9gGE-主程序

1 个答案:

答案 0 :(得分:0)

您的代码具有以下错误:

  • 您的代码在继承中是多余的:

    [1.] class Main(QtWidgets.QMainWindow, Ui_Dodaj_Ksiazke): 
    [2.]     def __init__(self):
    [3.]        QtWidgets.QMainWindow.__init__(self)
    [4.]        self.ui = Ui_Dodaj_Ksiazke()
    [5.]        self.ui.setupUi(self)
    

[1.]行中,您是从Ui_dodaj_autora继承而来的,在[4.]行中,您是在创建该类的对象,为什么要这样做呢?意义上,因此您必须使用以下两个选项之一:

class Main(QtWidgets.QMainWindow, Ui_Dodaj_Ksiazke):
    def __init__(self):
        super(Main, self).__init__()
        self.setupUi(self)

class Main(QtWidgets.QMainWindow):
    def __init__(self):
        super(Main, self).__init__()
        self.ui = Ui_Dodaj_Ksiazke()
        self.ui.setupUi(self)
  • 另一方面,当您使用Qt Designer时,您选择模板,因此当您要使用一个类时,它必须与该选择相对应,例如,在Ui_Dodaj_Ksiazke的情况下,您使用了主窗口模板,因此您必须使用QMainWindow,在Ui_dodaj_autora的情况下,您使用的是带有按钮底部的对话框,因此您应该使用QDialog

  • QDialogBu​​ttonBox必须将接受和拒绝的信号连接到QDialog的接受和拒绝插槽。

  • 使用QDialog对话框时要求用户提供数据时,您必须使用exec_()来知道请求是否被接受。

    < / li>

考虑到上述情况,可获得以下答案:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from debug_GUI import Ui_Dodaj_Ksiazke, Ui_dodaj_autora


class Autor(QtWidgets.QDialog, Ui_dodaj_autora):
    def __init__(self):
        super(Autor, self).__init__()
        self.setupUi(self)
        self.buttonBox.accepted.connect(self.accept)
        self.buttonBox.rejected.connect(self.reject)


class Main(QtWidgets.QMainWindow, Ui_Dodaj_Ksiazke):
    def __init__(self):
        super(Main, self).__init__()
        self.setupUi(self)

    def pole_autor(self):
        self.autor.addItems(["Wybierz autora...", "Dodaj autora..."])
        self.autor.currentTextChanged.connect(self.autorchange)

    @QtCore.pyqtSlot(str)
    def autorchange(self, text):
        if text == "Dodaj autora...":
            print("Dodawanie autora")
            okno = Autor()
            if okno.exec_() == QtWidgets.QDialog.Accepted:
                print(okno.lineEdit.text(), okno.lineEdit_2.text())


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = Main()
    window.pole_autor()
    window.show()
    sys.exit(app.exec_())
相关问题