Skip to main content
 首页 » 编程设计

python之PyQt:从小部件类外部访问启用状态

2025年04月02日9lidabo

我构建了一个 PyQt 界面,现在我想切换它的一些小部件的事件/非事件状态,或者从小部件类外部与它们交互。 我有主窗口类及其所有小部件和类外的函数 - 此处连接到 buttton1。我在此示例中的目标是通过按 button1 来启用 button2。

使用下面的代码,我收到错误消息,即我的 Ui_MainWindow 类没有属性 button1。

代码:

from PyQt4 import QtCore, QtGui 
 
def toggle(): 
     Ui_MainWindow.button2.setEnabled(True) 
 
class Ui_MainWindow(object): 
     def setupUi(self, MainWindow): 
         MainWindow.setObjectName(_fromUtf8("MainWindow")) 
         self.button1 = QtGui.QPushButton(self.centralwidget) 
         self.button1.clicked.connect(toggle) 
         self.button2 = QtGui.QPushButton(self.centralwidget) 
         self.button2.setEnabled(False) 
         QtCore.QMetaObject.connectSlotsByName(MainWindow)        
 
if __name__ == "__main__": 
 
    import sys 
    app = QtGui.QApplication(sys.argv) 
    MainWindow = QtGui.QMainWindow() 
    ui = Ui_MainWindow() 
    ui.setupUi(MainWindow) 
    MainWindow.show() 
    sys.exit(app.exec_()) 

直到知道我避免使用类,因为老实说,它们对我来说非常抽象。我想这就是我在这里失败的原因。我想我正在以错误的方式访问小部件类。

如果有人能指出正确的方向,我将不胜感激。我阅读了类似问题的所有答案 - 但它并没有帮助我找到解决方案。

请您参考如下方法:

我假设您在 Qt Designer 中为示例设计了 ui,并使用 pyuic4 将其转换为 python 模块。您需要做的是将此模块导入您的主脚本,然后创建一个加载 ui 的 MainWindow 类。如果操作正确,Qt Designer 中的所有小部件最终都将作为此类的属性。然后,您可以向控制与小部件交互的类添加方法。

首先,重新生成您的 ui 模块并将其保存为 mainwindow_ui.py:

pyuic4 -o mainwindow_ui.py mainwindow.ui 

然后像这样创建一个主脚本:

from PyQt4 import QtCore, QtGui 
from mainwindow_ui import Ui_MainWindow 
 
class MainWindow(QtGui.QMainWindow, Ui_MainWindow): 
    def __init__(self): 
         super(MainWindow, self).__init__() 
         self.setupUi(self) 
         self.button1.clicked.connect(self.toggle) 
         self.button2.setEnabled(False) 
 
    def toggle(self): 
         self.button2.setEnabled(not self.button2.isEnabled()) 
 
if __name__ == "__main__": 
 
    import sys 
    app = QtGui.QApplication(sys.argv) 
    window = MainWindow() 
    window.show() 
    sys.exit(app.exec_()) 

希望这段代码足够简单,不言自明。但是,如果您想要一些演练教程,我建议您试试这个:

PS: 这是 mainwindow.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>279</width> 
    <height>90</height> 
   </rect> 
  </property> 
  <property name="windowTitle"> 
   <string>MainWindow</string> 
  </property> 
  <widget class="QWidget" name="centralwidget"> 
   <layout class="QHBoxLayout" name="horizontalLayout"> 
    <item> 
     <widget class="QPushButton" name="button1"> 
      <property name="text"> 
       <string>Button 1</string> 
      </property> 
     </widget> 
    </item> 
    <item> 
     <widget class="QPushButton" name="button2"> 
      <property name="text"> 
       <string>Button 2</string> 
      </property> 
     </widget> 
    </item> 
   </layout> 
  </widget> 
  <widget class="QMenuBar" name="menubar"> 
   <property name="geometry"> 
    <rect> 
     <x>0</x> 
     <y>0</y> 
     <width>279</width> 
     <height>22</height> 
    </rect> 
   </property> 
  </widget> 
  <widget class="QStatusBar" name="statusbar"/> 
 </widget> 
 <resources/> 
 <connections/> 
</ui>