如何通过pyqt中的代码(不需要手动单击)单击关闭按钮?

2024-09-26 18:19:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在寻找以下功能:

self.ui.pushButton_connect2.click()

红十字按钮


Tags: self功能ui按钮clickpushbuttonconnect2
1条回答
网友
1楼 · 发布于 2024-09-26 18:19:06

你可以使用QTest,只需找到按钮的位置。只要用按钮所连接的方法替换keypressEvent

from PyQt5.Qt import *

class Window(QWidget):
    def __init__(self,parent=None):
        super(Window,self).__init__(parent)

    def keyPressEvent(self, QKeyEvent):
        if QKeyEvent.key() == Qt.Key_C:
            QTest.mousePress(self,Qt.LeftButton,Qt.KeyboardModifierMask,pos=QPoint(200,150))

    def mousePressEvent(self, event):
        print(event.pos())
        super().mousePressEvent(event)



app = QApplication([])
w = Window()
w.show()
app.exit(app.exec_())`

相关问题 更多 >

    热门问题