将按钮连接更改为另一个功能
Posted
技术标签:
【中文标题】将按钮连接更改为另一个功能【英文标题】:Changing a button connection to another function 【发布时间】:2017-09-28 14:23:34 【问题描述】:我有一个类似于幻灯片放映的应用程序:我有一个按钮可以在单击时更改背景图片。我还有第二个按钮可以帮助返回上一张图片。问题是,第一个按钮在节目结束时获得了另一个功能,但在那之后,当我单击back
按钮时,我无法将功能更改回前一个功能。
我的代码看起来有点像这样,我希望这能说明我的问题:
class SecondWindow(TemplateBaseClass):
def back(self):
self.first.n = self.first.n-2
self.hide()
self.first.show()
self.first.nextPicture()
def __init__(self):
TemplateBaseClass.__init__(self)
self.ui = WindowTemplate()
self.ui.setupUi(self)
self.first = MainWindow(self)
self.first.showFullScreen()
self.ui.pushButton.clicked.connect(lambda x:self.back())
class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
def showSecond(self):
#QTimer.singleShot(25, MainWindow)
self.second.showFullScreen()
self.hide()
def back(self):
if self.n >= 2:
self.n = self.n-2
self.notBack = False
self.nextPicture()
# I tried in several places like here, but it does not work
self.ui.end_button.clicked.connect(lambda x:self.nextPicture())
def nextPicture(self):
print(self.n)
if self.n == 0:
self.ui.bg_widget.setStyleSheet("background-image: url(:/ux/img0.png);\nbackground-repeat:no-repeat;")
elif self.n ==1 :
self.ui.bg_widget.setStyleSheet("background-image: url(:/ux/img1.png);\nbackground-repeat:no-repeat;")
elif self.n == 2:
self.ui.bg_widget.setStyleSheet("background-image: url(:/ux/img2.png);\nbackground-repeat:no-repeat;")
if self.notBack:
self.ui.end_button.clicked.connect(lambda x:self.showSecond())
else:
self.ui.end_button.clicked.connect(lambda x:self.nextPicture())
self.n +=1
self.notBack = True
def __init__(self, second):
QtGui.QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.second = second
self.setWindowTitle('pyqtgraph example: Qt Designer')
self.ui=uic.loadUi(uiFile, self)
self.setupUi(self)
self.setWindowFlags(QtCore.Qt.CustomizeWindowHint)
self.showFullScreen()
self.n = 1
self.notBack = True
self.ui.end_button.clicked.connect(lambda x:self.nextPicture())
self.ui.backButton.clicked.connect(lambda x:self.back())
【问题讨论】:
【参考方案1】:一个简单的disconnect()
就解决了这个问题:
def back(self):
if self.n >= 2:
self.n = self.n-2
self.notBack = False
self.nextPicture()
self.ui.end_button.disconnect()
self.ui.end_button.clicked.connect(lambda x:self.nextPicture())
【讨论】:
以上是关于将按钮连接更改为另一个功能的主要内容,如果未能解决你的问题,请参考以下文章