有没有办法确保某个功能在另一个功能启动之前运行到最后?

时间:2019-03-22 22:16:41

标签: python python-3.x pyqt pyqt5

这是我的工作:我对库中的不同部分有一个png计划。在PyQt5中,每个部分都有一个按钮,当按下该按钮时,会将基本png替换为突出显示相应部分的副本。

请参见下面的代码,以了解穴居人对该问题的解决方案。

它可以工作,但是如果我单击cdLoc按钮但不等它结束并按dvdLoc,则dvdLoc将运行通过,然后将运行cdLoc,突出显示错误的部分。

所以我的问题(除了为更好地完成这项工作的任何建议之外)是,是否有一种方法可以使函数一次运行一个,或者在我运行特定功能时禁止启动其他任何功能?

编辑:我不添加png文件,因此我将其打印按钮输出到控制台。如果您单击Cds,然后单击第一个DVD,则单击Dvds,它将在末尾打印Cds。

from PyQt5 import QtCore, QtGui, QtWidgets, QtTest
from PyQt5.QtGui import QIcon, QPixmap

import time

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 662)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(10, 10, 640, 562))
        self.label.setFrameShape(QtWidgets.QFrame.Box)
        self.label.setText("")
        self.label.setObjectName("label")
        pixmap = QPixmap('blank.png') 
        self.label.setPixmap(pixmap)
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(160, 580, 334, 32))
        font = QtGui.QFont()
        font.setFamily("Segoe UI")
        font.setPointSize(18)
        self.label_2.setFont(font)
        self.label_2.setObjectName("label_2")

        self.cds = QtWidgets.QPushButton(self.centralwidget)
        self.cds.setGeometry(QtCore.QRect(650, 130, 141, 23))
        self.cds.setObjectName("cds")
        self.dvd = QtWidgets.QPushButton(self.centralwidget)
        self.dvd.setGeometry(QtCore.QRect(650, 160, 141, 23))
        self.dvd.setObjectName("dvd")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
        self.cds.clicked.connect(self.cdLoc)
        self.dvd.clicked.connect(self.dvdLoc)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Library Plan"))
        self.label_2.setText(_translate("MainWindow", "Library plan"))
        self.cds.setText(_translate("MainWindow", "Cds"))
        self.dvd.setText(_translate("MainWindow", "Dvds"))

    def cdLoc(self):
        print('cd') # I had to replace my code but so it can be tested but the orignal code uses these two lines instead of printing : pixmap = Pixmap('pictureName.png') and self.label.setPixmap(pixmap)
        QtTest.QTest.qWait(1000)
        print('base')
        QtTest.QTest.qWait(500)
        print('cd')
        QtTest.QTest.qWait(1000)
        print('base')
        QtTest.QTest.qWait(500)
        print('cd')

    def dvdLoc(self):
        print('dvd')
        QtTest.QTest.qWait(1000)
        print('base')
        QtTest.QTest.qWait(500)
        print('dvd')
        QtTest.QTest.qWait(1000)
        print('base')
        QtTest.QTest.qWait(500)
        print('dvd')

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

  

enabled:bool

     

此属性保存是否启用了小部件

     

通常,启用的窗口小部件处理键盘和鼠标事件;禁用的小部件则不会。

import sys
from PyQt5.QtCore    import *
from PyQt5.QtGui     import *
from PyQt5.QtWidgets import *
from PyQt5           import QtTest


class Window(QWidget):
    def __init__(self):
        super(Window, self).__init__()

        self.label = QLabel()

        self.cdLocButton = QPushButton('cdLocButton')
        self.cdLocButton.clicked.connect(self.cdLoc)

        self.dvdLocButton = QPushButton('dvdLocButton')
        self.dvdLocButton.clicked.connect(self.dvdLoc)

        layout = QVBoxLayout(self)
        layout.addWidget(self.label)
        layout.addWidget(self.cdLocButton)
        layout.addWidget(self.dvdLocButton)        

    def cdLoc(self):
        self.dvdLocButton.setEnabled(False)                   # <---

        pixmap = QPixmap('Ok.png')
        self.label.setPixmap(pixmap)
        QtTest.QTest.qWait(1000)
        pixmap = QPixmap('im.png')
        self.label.setPixmap(pixmap)
        QtTest.QTest.qWait(500)
        pixmap = QPixmap('Ok.png')
        self.label.setPixmap(pixmap)
        QtTest.QTest.qWait(1000)
        pixmap = QPixmap('im.png')
        self.label.setPixmap(pixmap)
        QtTest.QTest.qWait(500)
        pixmap = QPixmap('Ok.png')
        self.label.setPixmap(pixmap)

        self.dvdLocButton.setEnabled(True)                    # <---

    def dvdLoc(self):
        self.cdLocButton.setEnabled(False)
        pixmap = QPixmap('exit.png')
        self.label.setPixmap(pixmap)
        QtTest.QTest.qWait(1000)
        pixmap = QPixmap('save.png')
        self.label.setPixmap(pixmap)
        QtTest.QTest.qWait(500)
        pixmap = QPixmap('exit.png')
        self.label.setPixmap(pixmap)
        QtTest.QTest.qWait(1000)
        pixmap = QPixmap('save.png')
        self.label.setPixmap(pixmap)
        QtTest.QTest.qWait(500)
        pixmap = QPixmap('exit.png')
        self.label.setPixmap(pixmap)
        self.cdLocButton.setEnabled(True)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())