pyside教程 Module_2_关闭按钮

阅读更多

 

我想下个QT designer。可是找不到。。冒是下QT Creator就行

http://qt-project.org/downloads

好吧。先装再说(d:\ide\qtcreator-2.4.1)

不行,得装QT SDK。。啊。1.7G, i am shattered

(补充:后来看到了PyQT这样一个好东西:http://www.riverbankcomputing.com/software/pyqt/download ,只要下载27.6M,就包含了QT, QT designer)

 

为什么要下。因为在看这篇文章:http://qt-project.org/wiki/PySideSimplicissimus_Module_2_CloseButton

等装好了再说吧。不知要下多久!

==

不必了。QT Designer只不过是生成了一个描述ui的xml文件。http://akabaila.pcug.org.au/pyside-data/quitter.ui

继续看,no blocker.

打开这个ui文件,发现里边定义了一个按钮,一个菜单栏,像是两行两列的布局,绑定了按钮和MainWindow。就当是给button加了ActionListener而MainWindow需要去实现这个Listener。

 

接下来把这个xml转成py,竟有此等功能#_!

pyside- uic quitter.ui - o ui_quitter.py

 

不识别的dos命令。把D:\Python27\Scripts加到System path.

好的,和教程写的一模一样

Traceback (most recent call last):
  File "D:\Python27\Scripts\pyside-uic-script.py", line 5, in
    from pkg_resources import load_entry_point
ImportError: No module named pkg_resources

需要装setuptools:http://pypi.python.org/pypi/setuptools ,找到setuptools-0.6c11.win32-py2.7.exe

自动安装到了D:\Python27\Lib\site-packages\目录下。。。

 

再次运行

pyside- uic quitter.ui - o ui_quitter.py

咚咚,迫不及待想看看生成的ui_quitter.py是什么样子

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'quitter.ui'
#
# Created: Thu Apr 12 23:27:33 2012
#      by: pyside-uic 0.2.13 running on PySide 1.1.0
#
# WARNING! All changes made in this file will be lost!

from PySide import QtCore, QtGui

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(526, 277)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtGui.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        spacerItem = QtGui.QSpacerItem(420, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.gridLayout.addItem(spacerItem, 0, 0, 1, 1)
        self.quitButton = QtGui.QPushButton(self.centralwidget)
        self.quitButton.setObjectName("quitButton")
        self.gridLayout.addWidget(self.quitButton, 0, 1, 1, 1)
        spacerItem1 = QtGui.QSpacerItem(20, 165, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
        self.gridLayout.addItem(spacerItem1, 1, 1, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 526, 29))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QObject.connect(self.quitButton, QtCore.SIGNAL("clicked()"), MainWindow.close)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
        self.quitButton.setText(QtGui.QApplication.translate("MainWindow", "&Quit", None, QtGui.QApplication.UnicodeUTF8))

 

原来pyside-uic是个代码生成工具,根据xml配置生成一个有头有脸的python类。明白了

 

 

 

主程序从自动生成的类继承而来:

#!/usr/bin/env python
# quitter.py - provide a button to quit this "program"

import sys

from PySide.QtGui import QMainWindow, QPushButton, QApplication
 
from ui_quitter import Ui_MainWindow

class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)
        
if __name__ == '__main__':
    app = QApplication(sys.argv)
    frame = MainWindow()
    frame.show()    
    app.exec_()
 

跑一下。


pyside教程 Module_2_关闭按钮_第1张图片

 

为什么没写任何代码。点button就可以关闭, 文章指出

The reason for its absence is that it has been created on the Qt Designer , which has a signal-slot editor. We used it surreptitiously(偷偷地) to connect PushButton.click() “signal” to MainWindow.close() “slot”. It is imbedded in the quitter.ui file and following the conversion, in the ui_quitter.py file. And from that file, we imported Ui_MainWindow class, which is one of the ancestors of our MainWindow class. We use Python’s multiple inheritance to facilitate use of methods in the ui_quitter.py module.

 

最后,结束之前让我再看一眼生成的xml代码(quitter.ui)



 MainWindow
 
  
   
    0
    0
    526
    277
   
  
  
   MainWindow
  
  
   
    
     
      
       Qt::Horizontal
      
      
       
        420
        20
       
      
     
    
    
     
      
       &Quit
      
     
    
    
     
      
       Qt::Vertical
      
      
       
        20
        165
       
      
     
    
   
  
  
   
    
     0
     0
     526
     29
    
   
  
  
 
 
 
  
   quitButton
   clicked()
   MainWindow
   close()
   
    
     479
     52
    
    
     262
     138
    
   
  
 

 

 

 

 

 

  • pyside教程 Module_2_关闭按钮_第2张图片
  • 大小: 7.1 KB
  • 查看图片附件

你可能感兴趣的:(pyside,qt,designer)