您好,登錄后才能下訂單哦!
????在使用pycharm進行編寫代碼的時候,發現信號設置了connect一個槽,但是測試發現沒有起作用,另外,在pycharm編輯器上connect提示沒有這個函數,總是找不到原因,后面決定還是對碰到相應的知識點需要做對應的測試用例進行學習和驗證。
????下面這個是對信號+槽的一個典型實例。可以正常按預期運行,在pycharm中connect也顯示沒有這個函數,應該就是pycharm本身設置和提示的問題,后面忽略它。所以有時候也不能鉆牛角尖,pycharm也僅僅是個編輯器而已。
#-*-?coding:utf-8?-*- ''' Signal?&?Slot ''' __author__?=?'Tony?Zhu' import?sys from?PyQt5.QtCore?import?Qt from?PyQt5.QtWidgets?import?(QWidget,?QLCDNumber,?QSlider,QGridLayout,QLabel,QHBoxLayout,?QGroupBox, ????QVBoxLayout,?QApplication,QProgressBar,QPushButton,QMessageBox) class?SignalSlot(QWidget): ????def?__init__(self): ????????super(SignalSlot,self).__init__() ????????self.initUI() ????def?initUI(self): ????????self.controlsGroup?=?QGroupBox("運行樣本") ????????self.lcdNumber?=?QLCDNumber(self) ????????self.slider?=?QSlider(Qt.Horizontal,?self) ????????self.pBar?=?QProgressBar(self) ????????vbox?=?QVBoxLayout() ????????vbox.addWidget(self.pBar) ????????vbox.addWidget(self.lcdNumber) ????????vbox.addWidget(self.slider) ????????self.controlsGroup.setLayout(vbox) ????????controlsLayout?=?QGridLayout() ????????self.label1?=?QLabel("保存狀態:") ????????self.saveLabel?=?QLabel() ????????self.label2?=?QLabel("運行狀態:") ????????self.runLabel?=?QLabel() ????????self.buttonSave?=?QPushButton("保存") ????????self.buttonRun?=?QPushButton("運行") ????????self.buttonStop?=?QPushButton("停止") ????????self.buttonDisconnect?=?QPushButton("解除關聯") ????????self.buttonConnect?=?QPushButton("綁定關聯") ????????controlsLayout.addWidget(self.label1,0,0) ????????controlsLayout.addWidget(self.saveLabel,0,1) ????????controlsLayout.addWidget(self.label2,1,0) ????????controlsLayout.addWidget(self.runLabel,1,1) ????????controlsLayout.addWidget(self.buttonSave,2,0) ????????controlsLayout.addWidget(self.buttonRun,2,1) ????????controlsLayout.addWidget(self.buttonStop,2,2) ????????controlsLayout.addWidget(self.buttonDisconnect,3,0) ????????controlsLayout.addWidget(self.buttonConnect,3,1) ????????layout?=?QHBoxLayout() ????????layout.addWidget(self.controlsGroup) ????????layout.addLayout(controlsLayout) ????????self.setLayout(layout) ????????self.buttonRun.clicked.connect(self.buttonSave.clicked) ????????self.slider.valueChanged.connect(self.pBar.setValue) ????????self.slider.valueChanged.connect(self.lcdNumber.display) ????????self.buttonSave.clicked.connect(self.showMessage) ????????self.buttonRun.clicked.connect(self.showMessage) ????????self.buttonDisconnect.clicked.connect(self.unbindConnection) ????????self.buttonConnect.clicked.connect(self.bindConnection) ????????self.buttonStop.clicked.connect(self.stop) ????????self.setGeometry(300,?500,?500,?180) ????????self.setWindowTitle('信號和槽') ????def?showMessage(self): ????????if?self.sender().text()?==?"保存": ????????????self.saveLabel.setText("Saved") ????????elif?self.sender().text()?==?"運行": ????????????self.saveLabel.setText("Saved") ????????????self.runLabel.setText("Running") ????def?unbindConnection(self): ????????self.slider.valueChanged.disconnect() ????def?bindConnection(self): ????????self.slider.valueChanged.connect(self.pBar.setValue) ????????self.slider.valueChanged.connect(self.lcdNumber.display) ????def?stop(self): ????????self.saveLabel.setText("") ????????self.runLabel.setText("") if?__name__?==?'__main__': ????app?=?QApplication(sys.argv) ????ex?=?SignalSlot() ????ex.show() ????sys.exit(app.exec_())
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。