【问题标题】:How do you simulate a QPushButton press using code如何使用代码模拟 QPushButton 按下
【发布时间】:2020-06-09 16:22:54
【问题描述】:

我想让一个按钮点击的处理程序模拟另一个按钮点击有没有办法使用本机信号/插槽来做到这一点?单击按钮#1时,我的测试代码(我正在寻找的行为)应该执行#1处理程序并从按钮#2发送按钮按下(信号),该按钮执行#2处理程序并从按钮发送按钮按下(信号) #1并重复。错误遵循我尝试过的两件事:

import time
import sys
from PyQt5.QtWidgets import QMainWindow, QPushButton, QApplication


class Example(QMainWindow):

    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):

        self.btn1 = QPushButton("Button 1", self)
        self.btn1.move(30, 50)

        self.btn2 = QPushButton("Button 2", self)
        self.btn2.move(150, 50)

        self.btn1.clicked.connect(self.button1Clicked)
        self.btn2.clicked.connect(self.button2Clicked)

        self.statusBar()

        self.setGeometry(300, 300, 290, 150)
        self.setWindowTitle('Event sender')
        self.show()

    def button1Clicked(self):

        self.statusBar().showMessage('Button #1 was pressed')
        time.sleep(5)
        self.btn2.emit()        # QPushButton' object has no attribute 'emit'
        self.btn2.clicked()     # native Qt signal is not callable

    def button2Clicked(self):

        self.statusBar().showMessage('Button #2 was pressed')
        time.sleep(5)
        self.btn1.emit()
        self.btn1.clicked()


if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

【问题讨论】:

  • 一些反馈???

标签: python pyqt pyqt5 qpushbutton


【解决方案1】:

您不应使用 time.sleep,因为它会阻塞 GUI 事件循环。如果您想在 T 时间后发出 clicked 信号,则必须使用 QTimer 调用 button.clicked.emit()

QTimer.singleShot(T, lambda: button.clicked.emit())

可以简化为:

QTimer.singleShot(T, button.clicked.emit)

在你的情况下:

def button1Clicked(self):
    self.statusBar().showMessage("Button #1 was pressed")
    QTimer.singleShot(5000, self.btn2.clicked.emit)

def button2Clicked(self):
    self.statusBar().showMessage("Button #2 was pressed")
    QTimer.singleShot(5000, self.btn1.clicked.emit)

另一方面,建议仅在信号所属的类中发出信号,因此对于按钮,您可以使用click() 方法来执行相同操作:

def button1Clicked(self):
    self.statusBar().showMessage("Button #1 was pressed")
    QTimer.singleShot(5000, self.btn2.click)

def button2Clicked(self):
    self.statusBar().showMessage("Button #2 was pressed")
    QTimer.singleShot(5000, self.btn1.click)

这会导致信号发出但看不到按下按钮的效果,如果你想看到效果那么你必须使用animateClick()方法:

def button1Clicked(self):
    self.statusBar().showMessage("Button #1 was pressed")
    QTimer.singleShot(5000, self.btn2.animateClick)

def button2Clicked(self):
    self.statusBar().showMessage("Button #2 was pressed")
    QTimer.singleShot(5000, self.btn1.animateClick)

另一种方法是使用发送 QMouseEvent 到小部件:

def send_mouse_event(widget, timeout=100):
    lp = widget.rect().center()
    press_event = QMouseEvent(
        QEvent.MouseButtonPress, lp, Qt.LeftButton, Qt.LeftButton, Qt.NoModifier
    )
    QCoreApplication.postEvent(widget, press_event)
    release_event = QMouseEvent(
        QEvent.MouseButtonRelease, lp, Qt.LeftButton, Qt.LeftButton, Qt.NoModifier
    )
    QTimer.singleShot(
        timeout, lambda: QCoreApplication.postEvent(widget, release_event)
    )
def button1Clicked(self):
    self.statusBar().showMessage("Button #1 was pressed")
    QTimer.singleShot(5000, lambda: send_mouse_event(self.btn2))

def button2Clicked(self):
    self.statusBar().showMessage("Button #2 was pressed")
    QTimer.singleShot(5000, lambda: send_mouse_event(self.btn1))

另一种解决方案是使用 QtTest.QTest:

from PyQt5.QtTest import QTest
# ...
class Example(QMainWindow):
    # ...
    def button1Clicked(self):
        self.statusBar().showMessage("Button #1 was pressed")
        QTimer.singleShot(
            5000, lambda: QTest.mouseClick(self.btn2, Qt.LeftButton, delay=100)
        )

    def button2Clicked(self):
        self.statusBar().showMessage("Button #2 was pressed")
        QTimer.singleShot(
            5000, lambda: QTest.mouseClick(self.btn1, Qt.LeftButton, delay=100)
        )

【讨论】:

    猜你喜欢
    • 2015-02-09
    • 2019-10-31
    • 2011-06-01
    • 2012-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-08
    • 1970-01-01
    相关资源
    最近更新 更多