Python -- Gui编程 -- Qt库的使用 -- 配置资源文件

时间:2022-07-08 14:43:45

1.源文件(qtRes.py)

 import sys
 from PyQt4 import QtCore, QtGui, uic

 class MyDialog(QtGui.QDialog):
     def __init__(self):
         QtGui.QDialog.__init__(self)
         uic.loadUi('qtRes.ui', self)

 class MyWindow(QtGui.QWidget):
     def __init__(self):
         QtGui.QWidget.__init__(self)
         self.setWindowTitle('QtRes')
         self.resize(485, 300)

         self.btn = QtGui.QPushButton('Click Here')

         self.grid = QtGui.QGridLayout()
         self.grid.addWidget(self.btn)
         self.setLayout(self.grid)

         self.connect(self.btn, QtCore.SIGNAL('clicked()'), self.onBtn)

     def onBtn(self):
         mydialog = MyDialog()
         r = mydialog.exec_()
         if r:
             self.btn.setText(mydialog.txt.text())

 app = QtGui.QApplication(sys.argv)
 mywindow = MyWindow()
 mywindow.show()
 app.exec_()

2.资源文件(qtRes.ui) -- 使用QT设计师编辑,

 <?xml version="1.0" encoding="UTF-8"?>
 <ui version="4.0">
  <class>Dialog</class>
  <widget class="QDialog" name="Dialog">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>359</width>
     <height>212</height>
    </rect>
   </property>
   <property name="windowTitle">
    <string>Dialog</string>
   </property>
   <widget class="QDialogButtonBox" name="buttonBox">
    <property name="geometry">
     <rect>
      <x>-30</x>
      <y>140</y>
      <width>341</width>
      <height>32</height>
     </rect>
    </property>
    <property name="orientation">
     <enum>Qt::Horizontal</enum>
    </property>
    <property name="standardButtons">
     <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
    </property>
   </widget>
   <widget class="QLabel" name="lbl">
    <property name="geometry">
     <rect>
      <x>80</x>
      <y>60</y>
      <width>54</width>
      <height>12</height>
     </rect>
    </property>
    <property name="text">
     <string>Input</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="txt">
    <property name="geometry">
     <rect>
      <x>190</x>
      <y>60</y>
      <width>113</width>
      <height>20</height>
     </rect>
    </property>
   </widget>
  </widget>
  <resources/>
  <connections>
   <connection>
    <sender>buttonBox</sender>
    <signal>accepted()</signal>
    <receiver>Dialog</receiver>
    <slot>accept()</slot>
    <hints>
     <hint type="sourcelabel">
      <x>248</x>
      <y>254</y>
     </hint>
     <hint type="destinationlabel">
      <x>157</x>
      <y>274</y>
     </hint>
    </hints>
   </connection>
   <connection>
    <sender>buttonBox</sender>
    <signal>rejected()</signal>
    <receiver>Dialog</receiver>
    <slot>reject()</slot>
    <hints>
     <hint type="sourcelabel">
      <x>316</x>
      <y>260</y>
     </hint>
     <hint type="destinationlabel">
      <x>286</x>
      <y>274</y>
     </hint>
    </hints>
   </connection>
  </connections>
 </ui>

Python -- Gui编程 -- Qt库的使用 -- 配置资源文件