PyQT:PushButton在禁用时接收命令

时间:2016-09-09 10:58:12

标签: python qt pyqt pyqt4 qpushbutton

我遇到了这个问题并尝试将其分解为我能想象到的最简单的代码:我使用Qt Designer V4.8.7创建了一个GUI,它只包含一个带有所有默认设置的pushButton。它被称为“Test2.ui'”。当按下按钮时,它应该被禁用,在终端中打印一些内容然后再次启用。发生的事情是,我能够点击禁用的pushButton,它会在我点击的同时重复所有打印次数。当我将按钮设置为不可见而不是禁用它时,这甚至可以工作。我在互联网上发现了类似的问题,但没有一个解决方案对我有用 - 它让我发疯。有人有想法吗?

from __future__ import division, print_function
import sys
from PyQt4 import QtCore, QtGui, uic
from PyQt4.QtCore import QTimer
from time import sleep

qtCreatorFile = "Test2.ui" # Enter file here.

Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)


class MyApp(QtGui.QMainWindow, Ui_MainWindow):

    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)
        self.pushButton.pressed.connect(self.Test_Function)


    def Test_Function(self):
        self.pushButton.setEnabled(False)
        QtGui.QApplication.processEvents()
        print('Test 1')
        sleep(1)
        print('Test 2')
        sleep(1)
        self.pushButton.setEnabled(True)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = MyApp()
    window.show()
    sys.exit(app.exec_())

以下是' Test2.ui'

的代码
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>445</width>
    <height>393</height>
   </rect>
  </property>
  <property name="mouseTracking">
   <bool>false</bool>
  </property>
  <property name="focusPolicy">
   <enum>Qt::ClickFocus</enum>
  </property>
  <property name="acceptDrops">
   <bool>false</bool>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QPushButton" name="pushButton">
    <property name="enabled">
     <bool>true</bool>
    </property>
    <property name="geometry">
     <rect>
      <x>160</x>
      <y>150</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>Test</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>445</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

1 个答案:

答案 0 :(得分:1)

您的示例代码无效,因为测试功能会阻止gui。虽然它是阻塞的,但按钮的禁用状态未正确更新,因此仍然可以发出clicked信号。避免阻止gui的最好方法是在单独的线程中完成工作:

class Worker(QtCore.QObject):
    finished = QtCore.pyqtSignal()

    def run(self):
        print('Test 1')
        sleep(1)
        print('Test 2')
        sleep(1)
        self.finished.emit()

class MyApp(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self):
        ...

        self.pushButton.pressed.connect(self.handleButton)

        self.thread = QtCore.QThread(self)
        self.worker = Worker()
        self.worker.moveToThread(self.thread)
        self.worker.finished.connect(self.handleFinished)
        self.thread.started.connect(self.worker.run)

    def handleButton(self):
        self.pushButton.setEnabled(False)
        self.thread.start()

    def handleFinished(self):
        self.thread.quit()
        self.thread.wait()
        self.pushButton.setEnabled(True)