我遇到了这个问题,并试图将其分解为我能想到的最简单的代码:我使用Qt Designer V4.8.7创建了一个GUI,该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>
最佳答案
您的示例代码将无法正常工作,因为测试功能会阻止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)
关于python - PyQT:PushButton禁用时接收命令,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39410037/