【问题标题】:How do I use signals from a QWidget to tell the main window to execute a function?如何使用来自 QWidget 的信号来告诉主窗口执行功能?
【发布时间】:2017-01-24 01:17:45
【问题描述】:

每当在确认弹出窗口上按下回车按钮时,我都会尝试运行函数 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_())

【问题讨论】:

  • 什么是 inputAttendance?
  • 下面的答案没有用吗?它应该可以完美运行。
  • @Y.Melo 感谢您的回复!我现在正忙着工作/要回学校,所以我明天晚上看看你的回复。

标签: python pyqt pyqt5


【解决方案1】:

先生您好,这是一个非常好的问题,让我们这样总结一下。

  1. 假设我们有两个类,Widget 类和 MainWindow 类。
  2. 小部件将包含两个信号,一个信号从自身运行一个方法,另一个信号将从 MainWindow 运行一个方法。
  3. 我们将在小部件内部连接的第一个信号,从小部件发出并运行一个方法。
  4. 第二个将连接到 MainWindow 内,我们有一个类的小部件实例,并将从 MainWindow 运行一个方法

让我们看看这个代表我刚刚试图总结的小例子。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-30
    • 1970-01-01
    • 2012-11-21
    • 2011-10-06
    • 1970-01-01
    相关资源
    最近更新 更多