描述:btn-1是正常的按键,按下就会执行固定任务,而btn-2的功能是禁用btn-1,当点击btn-2后就会禁用btn-1,btn-1就不用执行先前的功能了
from PyQt5.Qt import *
import sys
class MyWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("控件交互状态测试")
self.setStyleSheet('background-color:skyblue')
self.resize(300,400)
self.move(700,200)
self.setup_ui()
def setup_ui(self):
btn1 = QPushButton(self)
btn1.setText('BTN-1')
btn1.move(50,50)
btn1.resize(80,40)
btn1.setStyleSheet('background-color:green')
btn2 = QPushButton(self)
btn2.setText('BTN-2')
#btn2.move(40,30)
btn2.resize(70,30)
btn2.setStyleSheet('background-color:red')
btn2.pressed.connect(lambda:btn1.setEnabled(False))
btn2.pressed.connect(lambda:print("btn2成功禁用btn1"))
btn1.pressed.connect(lambda:print('有交互:'))
#btn1.setEnabled(False)
if btn1.isEnabled()==True:
print('BTN-1按键可用,现在可以通过按BTN-2来禁用BTN-1')
else:print('按键被禁用')
if __name__ == '__main__':
app = QApplication(sys.argv)
myWindow = MyWindow()
myWindow.show()
sys.exit(app.exec_())