我在Python中创建了一个GUI,它包含一个包含3个单独小部件的堆栈小部件,loginBackend,editorBackend,mapBackend。它们都是单独的类,也叫loginBackend,editorBackend,mapBackend。每个都是从一个名为mainBackend的类和主窗口小部件中调用的。这个想法是用户将登录(使用loginBackend小部件),然后将能够访问其余的小部件。问题是每个其他小部件都需要登录信息才能连接到数据库。出于某种原因,当我从editorBackend类调用它时,我无法正确地返回信息。我想做的就是这样的事情
这是editorBackend
from loginBackend import loginBackend
class editorBackend(QWidget, editordesign.Ui_Form):
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self)
self.initUI()
def initUI(self):
self.submitPushButton.clicked.connect(self.Insert)
def Insert(self):
login = loginBackend()
url = login.creds()
print('url: ' + url)
这是loginBackend
class loginBackend(QWidget, logindesign.Ui_Login):
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self)
def creds(self):
username = self.userLineEdit.text()
password = self.passLineEdit.text()
host = self.selHostLabel.text()
port = self.selPortLabel.text()
sid = self.selSIDLabel.text()
cred = username + '/' + password + '@' + host + ':' + port + '/' + sid
print('creds: '+ cred)
return cred
然后,这是调用这两个窗口并将它们放入堆叠小部件的主窗口。
from PyQt5.QtWidgets import QApplication,QMainWindow
import sys
from loginBackend import loginBackend
from mapBackend import mapBackend
from editorBackend import editorBackend
from editTNSnamesBackend import editTNSnamesBackend
import mainwindow
class mainBackend(QMainWindow, mainwindow.Ui_MainWindow):
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self)
self.initUI()
def initUI(self):
self.url = None
self.popList()
self.login = loginBackend()
self.mapping = mapBackend()
self.edit = editorBackend()
self.editTNS = editTNSnamesBackend()
self.popWidgets()
self.listWidget.itemClicked.connect(self.change)
self.actionTNS_Names.triggered.connect(self.editTNSnames)
def editTNSnames(self):
self.editTNS.show()
def change(self):
index = self.listWidget.currentRow()
self.stackedWidget.setCurrentIndex(index)
self.url = self.login.creds()
print('url change. ' + self.url)
def popList(self):
widgets = ['Login', 'Edit Database', 'Edit Mapping']
self.listWidget.addItems(widgets)
def popWidgets(self):
self.stackedWidget.insertWidget(0,self.login)
self.stackedWidget.setCurrentWidget(self.login)
self.stackedWidget.insertWidget(1,self.edit)
self.stackedWidget.setCurrentWidget(self.edit)
self.stackedWidget.insertWidget(2,self.mapping)
self.stackedWidget.setCurrentWidget(self.mapping)
self.stackedWidget.setCurrentIndex(0)
self.listWidget.setCurrentRow(0)
def main():
app = 0
app = QApplication(sys.argv)
main_window = mainBackend()
main_window.show()
app.exec_()
if __name__ == "__main__":
main()
loginBackend和主窗口中的print语句打印出我需要的内容,但我无法在editorBackend中打印任何内容。有没有人对如何解决这个问题或者解决方法有任何建议?
设计师档案(缩写):
logindesign classes:
class Ui_Login(object):
def setupUi(self, Login):
Login.setObjectName("Login")
...
...
...
def retranslateUi(self, Login):
_translate = QtCore.QCoreApplication.translate
editordesign课程:
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
...
...
...
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
主窗口类:
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
...
...
...
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
答案 0 :(得分:0)
所以我通过在我需要信息的类中创建一个函数来解决我的问题。然后我可以将它作为参数从主窗口传递(我知道我可以获取值)。如下所示。
在editorBackend
中创建一个函数from loginBackend import loginBackend
class editorBackend(QWidget, editordesign.Ui_Form):
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self)
self.initUI()
self.url = None
def initUI(self):
self.submitPushButton.clicked.connect(self.Insert)
#### NEW FUNCTION ####
def getUrl(self, url):
self.url = url
print('getUrl '+url)
######################
def Insert(self):
print('url: ' + self.url)
将登录信息从主窗口发送到editorBackend
from PyQt5.QtWidgets import QApplication,QMainWindow
import sys
from loginBackend import loginBackend
from mapBackend import mapBackend
from editorBackend import editorBackend
from editTNSnamesBackend import editTNSnamesBackend
import mainwindow
class mainBackend(QMainWindow, mainwindow.Ui_MainWindow):
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self)
self.initUI()
def initUI(self):
self.url = None
self.popList()
self.login = loginBackend()
self.mapping = mapBackend()
self.edit = editorBackend()
self.editTNS = editTNSnamesBackend()
self.popWidgets()
self.listWidget.itemClicked.connect(self.change)
self.actionTNS_Names.triggered.connect(self.editTNSnames)
def editTNSnames(self):
self.editTNS.show()
def change(self):
index = self.listWidget.currentRow()
self.stackedWidget.setCurrentIndex(index)
self.url = self.login.creds()
print('url change. ' + self.url)
### PASS THE INFORMATION TO editorBackend ###
self.edit.getUrl(self.url)
#############################################
def popList(self):
widgets = ['Login', 'Edit Database', 'Edit Mapping']
self.listWidget.addItems(widgets)
def popWidgets(self):
self.stackedWidget.insertWidget(0,self.login)
self.stackedWidget.setCurrentWidget(self.login)
self.stackedWidget.insertWidget(1,self.edit)
self.stackedWidget.setCurrentWidget(self.edit)
self.stackedWidget.insertWidget(2,self.mapping)
self.stackedWidget.setCurrentWidget(self.mapping)
self.stackedWidget.setCurrentIndex(0)
self.listWidget.setCurrentRow(0)
def main():
app = 0
app = QApplication(sys.argv)
main_window = mainBackend()
main_window.show()
app.exec_()
if __name__ == "__main__":
main()