【发布时间】: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