将按钮连接更改为其他功能

时间:2017-09-28 14:23:34

标签: python pyqt5

我的应用程序有点像幻灯片放映:我有一个按钮,可以在单击时更改背景图片。我还有第二个按钮,可以帮助我回到上一张图片。问题是,第一个按钮在节目结束时获得另一个功能,但在此之后,当我单击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 个答案:

答案 0 :(得分:0)

一个简单的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())