我有一个文件,其中包含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数据库中,并且窗口应关闭。
我尝试使用,与主windoww相同,但失败并显示以下错误:
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.] 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.]
行中,您是创建该类的对象,为什么要这样做?这没有任何意义,因此您必须执行以下操作之一2种选择:
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
,您已经使用了Main Window模板,因此必须使用QMainWindow
,对于Ui_dodaj_autora
,您已使用“底部带有按钮的对话框”,因此应使用QDialog
。
QDialogButtonBox必须将接受和拒绝的信号连接到QDialog的接受和拒绝插槽。
使用QDialog
对话框时要求用户提供数据时,必须使用exec_()
知道请求是否被接受。
考虑到上述情况,可获得以下答案:
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_())
Python PyQt在表单之间发送数据 - python我有两种使用Qt Designer构建的表单。这两种形式利用here中描述的uic.loadUiType过程。我已将UI表单上载到this location以导入表单。主窗口有三个按钮。按下每个按钮时-尝试将哪个按钮传递给数字键盘表单-尝试使用信号和插槽,但不起作用。当NumPad窗体打开时,我需要使用它填充Field1、2或3,以便我可以将txtDatTo…
单击按钮后如何从组合框更改变量值? - python我正在使用未加权GPA计算器,并且对(Py)Qt Designer应用程序还是有点陌生。我遇到了一个问题,我不知道如何从ComboBoxes中获取结果以将其计算为名为gpa的变量。基本上,这就是我想发生的事情:如果letter_grade1 ComboBox是A +,则它将gpa加4.0如果letter_grade2 ComboBox是B,则它将gpa加…
如何使用pass语句? - python我正在学习Python,并且已经到达有关pass语句的部分。我正在使用的指南将其定义为通常用作占位符的Null语句。我仍然不完全明白那是什么意思。有人可以告诉我一个简单的/基本情况,在其中使用pass语句以及为什么需要它吗? 参考方案 假设您正在使用尚未实现的某些方法设计一个新类。class MyClass(object): def meth_a(self)…
Python-在PyQt5中循环创建按钮 - python我一直在尝试根据PyQt5中变量的值创建X个按钮,但是我的方法不起作用。我创建了一个带有循环的函数,其中X值为按钮的数量。此代码(功能已注释)有效:class Ui_MainWindow(object): def setupUi(self, MainWindow): def createButt…
Python uuid4,如何限制唯一字符的长度 - python在Python中,我正在使用uuid4()方法创建唯一的字符集。但是我找不到将其限制为10或8个字符的方法。有什么办法吗?uuid4()ffc69c1b-9d87-4c19-8dac-c09ca857e3fc谢谢。 参考方案 尝试:x = uuid4() str(x)[:8] 输出:"ffc69c1b" Is there a way to…