如何使用模块在 2 windows 之间传递自我
how to pass the self between 2 windows using modules
我正在学习 Python,使用 PyQT5 (Qt Designer) 的 3.7 版
我正在尝试使用模块定义标签的值,在 window 1 中它调用该函数并且它有效,但是当我打开 window 2 并单击它触发的按钮时该函数但在没有标签的情况下传递了当前 window (window 2) 的自身,生成以下错误:
Traceback (most recent call last):
File "\template\config.py", line 18, in returnMain
fun1(self)
File "\modulos\functions.py", line 7, in fun1
lbl = int(self.ui.lblTest.text())
AttributeError: 'Ui_window2' object has no attribute 'lblTest'
Preciso que ele use na função o self da window 1.
我在小文件中复制了错误,它一直有效,直到您单击第二个 window 中出现所述错误的按钮。
目录中的代码结构如下:
关注文件。
run.py(用于启动程序)
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
import sys
from template.main import MainScreen
if __name__ == "__main__":
app = QApplication(sys.argv)
main = MainScreen()
main.show()
sys.exit(app.exec_())
gui/window1.py (janela 校长)
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_window1(object):
def setupUi(self, win1):
win1.setObjectName("win1")
win1.resize(297, 219)
self.centralwidget = QtWidgets.QWidget(win1)
self.centralwidget.setObjectName("centralwidget")
self.btnConfig = QtWidgets.QPushButton(self.centralwidget)
self.btnConfig.setGeometry(QtCore.QRect(110, 150, 75, 23))
self.btnConfig.setObjectName("btnConfig")
self.lblTest = QtWidgets.QLabel(self.centralwidget)
self.lblTest.setGeometry(QtCore.QRect(120, 70, 47, 13))
self.lblTest.setObjectName("lblTest")
win1.setCentralWidget(self.centralwidget)
self.retranslateUi(win1)
QtCore.QMetaObject.connectSlotsByName(win1)
def retranslateUi(self, win1):
_translate = QtCore.QCoreApplication.translate
win1.setWindowTitle(_translate("win1", "Window 1"))
self.btnConfig.setText(_translate("win1", "Open config"))
self.lblTest.setText(_translate("win1", "0"))
gui/window2.py(设置window)
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_window2(object):
def setupUi(self, win2):
win2.setObjectName("win2")
win2.resize(222, 241)
self.centralwidget = QtWidgets.QWidget(win2)
self.centralwidget.setObjectName("centralwidget")
self.btnReturn = QtWidgets.QPushButton(self.centralwidget)
self.btnReturn.setGeometry(QtCore.QRect(70, 120, 75, 23))
self.btnReturn.setObjectName("btnReturn")
win2.setCentralWidget(self.centralwidget)
self.retranslateUi(win2)
QtCore.QMetaObject.connectSlotsByName(win2)
def retranslateUi(self, win2):
_translate = QtCore.QCoreApplication.translate
win2.setWindowTitle(_translate("win2", "Window 2"))
self.btnReturn.setText(_translate("win2", "Return Main"))
modulos/functions.py(函数在哪里)
from PyQt5.QtWidgets import *
from template import main
def fun1(self):
lbl = int(self.ui.lblTest.text())
lbl = (lbl+1)
self.ui.lblTest.setText(str(lbl))
template/config.py(我将在其中处理设置)
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from gui.window2 import *
from modulos.functions import fun1
class ConfigScreen(QMainWindow):
def __init__(self,*args,**argsv):
super(ConfigScreen,self).__init__(*args,**argsv)
self.ui = Ui_window2()
self.ui.setupUi(self)
self.ui.btnReturn.clicked.connect(self.returnMain)
def returnMain(self):
fun1(self)
self.close()
template/main.py(主程序模块)
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from gui.window1 import *
from template.config import ConfigScreen
from modulos.functions import fun1
class MainScreen(QMainWindow):
def __init__(self,*args,**argsv):
super(MainScreen,self).__init__(*args,**argsv)
self.ui = Ui_window1()
self.ui.setupUi(self)
self.ui.btnConfig.clicked.connect(self.openConfig)
fun1(self)
def openConfig(self):
self.config = ConfigScreen()
self.config.show()
有很多不同的方法可以解决这个问题。这里有一个可能的解决方案,尽量保持现有的结构。当请求 return 时,从 ConfigScreen 发出自定义信号以与 MainScreen 通信,并将其插槽连接到 fun1(self)
.
class ConfigScreen(QMainWindow):
returnRequested = QtCore.pyqtSignal()
def __init__(self,*args,**argsv):
super(ConfigScreen,self).__init__(*args,**argsv)
self.ui = Ui_window2()
self.ui.setupUi(self)
self.ui.btnReturn.clicked.connect(self.returnMain)
def returnMain(self):
self.returnRequested.emit()
self.close()
class MainScreen(QMainWindow):
def __init__(self,*args,**argsv):
super(MainScreen,self).__init__(*args,**argsv)
self.ui = Ui_window1()
self.ui.setupUi(self)
self.ui.btnConfig.clicked.connect(self.openConfig)
fun1(self)
def openConfig(self):
self.config = ConfigScreen()
self.config.returnRequested.connect(lambda: fun1(self))
self.config.show()
也functions.py不需要from template import main
.
我正在学习 Python,使用 PyQT5 (Qt Designer) 的 3.7 版
我正在尝试使用模块定义标签的值,在 window 1 中它调用该函数并且它有效,但是当我打开 window 2 并单击它触发的按钮时该函数但在没有标签的情况下传递了当前 window (window 2) 的自身,生成以下错误:
Traceback (most recent call last):
File "\template\config.py", line 18, in returnMain
fun1(self)
File "\modulos\functions.py", line 7, in fun1
lbl = int(self.ui.lblTest.text())
AttributeError: 'Ui_window2' object has no attribute 'lblTest'
Preciso que ele use na função o self da window 1.
我在小文件中复制了错误,它一直有效,直到您单击第二个 window 中出现所述错误的按钮。
目录中的代码结构如下:
关注文件。
run.py(用于启动程序)
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
import sys
from template.main import MainScreen
if __name__ == "__main__":
app = QApplication(sys.argv)
main = MainScreen()
main.show()
sys.exit(app.exec_())
gui/window1.py (janela 校长)
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_window1(object):
def setupUi(self, win1):
win1.setObjectName("win1")
win1.resize(297, 219)
self.centralwidget = QtWidgets.QWidget(win1)
self.centralwidget.setObjectName("centralwidget")
self.btnConfig = QtWidgets.QPushButton(self.centralwidget)
self.btnConfig.setGeometry(QtCore.QRect(110, 150, 75, 23))
self.btnConfig.setObjectName("btnConfig")
self.lblTest = QtWidgets.QLabel(self.centralwidget)
self.lblTest.setGeometry(QtCore.QRect(120, 70, 47, 13))
self.lblTest.setObjectName("lblTest")
win1.setCentralWidget(self.centralwidget)
self.retranslateUi(win1)
QtCore.QMetaObject.connectSlotsByName(win1)
def retranslateUi(self, win1):
_translate = QtCore.QCoreApplication.translate
win1.setWindowTitle(_translate("win1", "Window 1"))
self.btnConfig.setText(_translate("win1", "Open config"))
self.lblTest.setText(_translate("win1", "0"))
gui/window2.py(设置window)
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_window2(object):
def setupUi(self, win2):
win2.setObjectName("win2")
win2.resize(222, 241)
self.centralwidget = QtWidgets.QWidget(win2)
self.centralwidget.setObjectName("centralwidget")
self.btnReturn = QtWidgets.QPushButton(self.centralwidget)
self.btnReturn.setGeometry(QtCore.QRect(70, 120, 75, 23))
self.btnReturn.setObjectName("btnReturn")
win2.setCentralWidget(self.centralwidget)
self.retranslateUi(win2)
QtCore.QMetaObject.connectSlotsByName(win2)
def retranslateUi(self, win2):
_translate = QtCore.QCoreApplication.translate
win2.setWindowTitle(_translate("win2", "Window 2"))
self.btnReturn.setText(_translate("win2", "Return Main"))
modulos/functions.py(函数在哪里)
from PyQt5.QtWidgets import *
from template import main
def fun1(self):
lbl = int(self.ui.lblTest.text())
lbl = (lbl+1)
self.ui.lblTest.setText(str(lbl))
template/config.py(我将在其中处理设置)
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from gui.window2 import *
from modulos.functions import fun1
class ConfigScreen(QMainWindow):
def __init__(self,*args,**argsv):
super(ConfigScreen,self).__init__(*args,**argsv)
self.ui = Ui_window2()
self.ui.setupUi(self)
self.ui.btnReturn.clicked.connect(self.returnMain)
def returnMain(self):
fun1(self)
self.close()
template/main.py(主程序模块)
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from gui.window1 import *
from template.config import ConfigScreen
from modulos.functions import fun1
class MainScreen(QMainWindow):
def __init__(self,*args,**argsv):
super(MainScreen,self).__init__(*args,**argsv)
self.ui = Ui_window1()
self.ui.setupUi(self)
self.ui.btnConfig.clicked.connect(self.openConfig)
fun1(self)
def openConfig(self):
self.config = ConfigScreen()
self.config.show()
有很多不同的方法可以解决这个问题。这里有一个可能的解决方案,尽量保持现有的结构。当请求 return 时,从 ConfigScreen 发出自定义信号以与 MainScreen 通信,并将其插槽连接到 fun1(self)
.
class ConfigScreen(QMainWindow):
returnRequested = QtCore.pyqtSignal()
def __init__(self,*args,**argsv):
super(ConfigScreen,self).__init__(*args,**argsv)
self.ui = Ui_window2()
self.ui.setupUi(self)
self.ui.btnReturn.clicked.connect(self.returnMain)
def returnMain(self):
self.returnRequested.emit()
self.close()
class MainScreen(QMainWindow):
def __init__(self,*args,**argsv):
super(MainScreen,self).__init__(*args,**argsv)
self.ui = Ui_window1()
self.ui.setupUi(self)
self.ui.btnConfig.clicked.connect(self.openConfig)
fun1(self)
def openConfig(self):
self.config = ConfigScreen()
self.config.returnRequested.connect(lambda: fun1(self))
self.config.show()
也functions.py不需要from template import main
.