每当在confirmPopup窗口上按下回车按钮,我都会尝试运行函数inputAttendance(AthleteInfo)。此函数包含在我导入的另一个文件中,不在任何类中。 我遇到的一个问题是它似乎在运行
在信号发出之前self.confirmw.confirmAthlete.connect(inputAttendance(AthleteInfo))
。一旦inputAttendance()完成,我收到错误后整个窗口就会关闭
参数1具有意外类型'NoneType'
我尝试查找它,可能是我没有定义连接类型?
任何帮助都会非常感激,因为我已经坚持了很长一段时间。
编辑:InputAttendance()是一个更新电子表格的函数,该电子表格位于我导入但未包含在帖子中的另一个文件中,因为它与我的问题无关。我已经测试了这个功能并且它完美地工作,所以我确信它不会导致程序崩溃,而是如何调用它。抱歉混乱!
from PyQt5.QtWidgets import (QWidget, QPushButton, QLineEdit,
QInputDialog, QApplication, QLabel)
from PyQt5.QtCore import *
class Ex(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.le = QLineEdit(self)
self.le.move(500, 500)
self.le.returnPressed.connect(self.pushEnter)
self.setGeometry(1000, 1000, 1000, 1000)
self.setWindowTitle('Input dialog')
self.show()
def pushEnter(self):
text = self.le.text()
AthleteInfo = getID(text)
if (AthleteInfo == -1):
print ("Could nto find that ID")
else:
try:
self.confirmw =confirmPopup("Confirm Window")
except Exception in e:
print(e)
time.sleep(10)
self.confirmw.setGeometry(1000, 1000, 1000, 1000)
self.confirmw.show()
try:
self.confirmw.setWindowModality(Qt.ApplicationModal)
except Exception as e:
print(e)
time.sleep(5)
try: self.confirmw.confirmAthlete.connect(inputAttendance(AthleteInfo))
except Exception as e:
print(e)
time.sleep(5)
class confirmPopup(QWidget):
confirmAthlete = pyqtSignal(str)
def __init__(self, name):
super().__init__()
self.name = name
self.initUI()
def initUI(self):
lblName = QLabel(self.name, self, text = "Press enter to confirm")
def keyPressEvent(self, event):
keyPress = event.text()
if event.key() == Qt.Key_Enter or event.key() == Qt.Key_Return:
try:
#print("Emitting Signal")
self.confirmAthlete.emit("Yes")
except Exception as e:
print(e)
time.sleep(5)
if event.key() == Qt.Key_Backspace:
print("Backspace was pressed")
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Ex()
sys.exit(app.exec_())
答案 0 :(得分:-1)
Helloww先生,这是一个非常好的问题,让我们以这种方式总结。
让我们来看看这个小例子,它代表了我刚才总结的内容。
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QWidget
class MainWindow(QMainWindow):
myWidget = None
def __init__(self):
super(MainWindow, self).__init__()
self.myWidget = Widget()
self.setFixedSize(500,500)
self.myWidget.setFixedSize(500,500)
self.layout().addWidget(self.myWidget)
self.myWidget.signalExecuteAMainWindowFunction.connect(self.mainWindowFunction)
def mainWindowFunction(self):
print("running my MAINWINDOW function...")
class Widget(QWidget):
signalExecuteMyFunction = pyqtSignal()
signalExecuteAMainWindowFunction = pyqtSignal()
def __init__(self):
super(Widget, self).__init__()
self.signalExecuteMyFunction.connect(self.myFunction)
def mousePressEvent(self, QMouseEvent):
self.signalExecuteMyFunction.emit()
super(Widget, self).mousePressEvent(QMouseEvent)
def mouseMoveEvent(self, QMouseEvent):
self.signalExecuteAMainWindowFunction.emit()
super(Widget, self).mouseMoveEvent(QMouseEvent)
def myFunction(self):
print("running my WIDGET function...")
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec_())
Ps:还有很多很棒的方法可以做我做过的事情,比如变量名称,面向对象和组织,但它是你问的本质那件事。希望它能更清楚地说明它是如何工作的。 :d 强>