我遇到了這個問題,並試圖將其分解爲我能想象到的最簡單的代碼:我使用Qt Designer V4.8.7創建了一個GUI,它只包含一個具有所有默認設置的pushButton 。它被稱爲'Test2.ui'。按下該按鈕時,應該禁用該按鈕,在終端中打印某些內容,然後再次啓用。會發生什麼,我可以點擊禁用的pushButton,它會重複所有打印次數。當我將按鈕設置爲不可見而不是禁用它時,這甚至可以工作。我在網上發現了類似的問題,但沒有一個解決方案似乎適用於我 - 這讓我很生氣。任何人有想法?PyQT: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>
這可能不是python:但是在C++中Qt這是故意的:http://doc.qt.io/qt- 5/qabstractbutton.html啓用的按鈕仍然會收到鼠標事件。你應該檢查python文檔。似乎你需要檢查按鈕是否在你的點擊處理程序中啓用。 – Hayt
您能否提供'Test2.ui'的代碼?這將使測試更容易。 – Ian
我添加了.ui文件的代碼。使用.setDown替換.setEnabled命令導致相同的問題。 – Delf