PyQt:从窗口小部件类

时间:2016-11-06 11:51:56

标签: python pyqt pyqt4 signals-slots qwidget

我构建了一个PyQt接口,现在我想切换它的一些小部件的活动/非活动状态,或者从小部件类外部与它们进行交互。 我有主窗口类及其所有小部件,以及类外的函数 - 这里连接到buttton1。我在此示例中的目标是按button2启用button1

使用下面的代码,我收到的错误是我的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_())

直到知道我避免使用课程,因为老实说,它们对我来说非常抽象。我猜这就是我在这里失败的原因。我猜我是以错误的方式访问widget-class。

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

1 个答案:

答案 0 :(得分:1)

我假设您在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>