Skip to main content
 首页 » 编程设计

python之PyQT:按钮在禁用时接收命令

2025年04月02日8pander-it

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

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> 

请您参考如下方法:

您的示例代码将无法运行,因为测试函数会阻止图形用户界面。当它处于阻塞状态时,按钮的禁用状态没有正确更新,因此 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)