PySide学习笔记第十章-窗体小部件(widget)-下篇

来源:互联网 发布:试客小兵软件 编辑:程序博客网 时间:2024/05/08 23:28

本章会涉及 QtGui.QPixmap , QtGui.QLineEdit, QtGui.QSplitter and QtGui.QComboBox.等widget

一、QtGui.QPixmap 是一个操作图像的工具,是用来在屏幕上显示图像的

# -*- coding: utf-8 -*-"""ZetCode PySide tutorialIn this example, we dispay an imageon the window.author: Jan Bodnarwebsite: zetcode.comlast edited: August 2011"""import sysfrom PySide import QtGui, QtCoreclass Example(QtGui.QWidget):    def __init__(self):        super(Example, self).__init__()        self.initUI()    def initUI(self):        hbox = QtGui.QHBoxLayout(self)        pixmap = QtGui.QPixmap("play.png") # 加载图像为QPixmap对象        lbl = QtGui.QLabel(self)        lbl.setPixmap(pixmap) # 构造lable对象并显示该图像        hbox.addWidget(lbl)        self.setLayout(hbox)        self.setGeometry(300, 300, 280, 170)        self.setWindowTitle('Red Rock')        self.show()def main():    app = QtGui.QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())if __name__ == '__main__':    main()

二、QtGui.QLineEdit行编辑器,允许编辑或者输入一行纯文本,支持取消,剪切,黏贴,拖拽等操作

# -*- coding: utf-8 -*-"""ZetCode PySide tutorialThis example shows text whichis entered in a QtGui.QLineEditin a QtGui.QLabel widget.author: Jan Bodnarwebsite: zetcode.comlast edited: August 2011"""import sysfrom PySide import QtGui, QtCoreclass Example(QtGui.QWidget):    def __init__(self):        super(Example, self).__init__()        self.initUI()    def initUI(self):        self.lbl = QtGui.QLabel(self) # 构造一个lable用于显示行编辑器输入的结果        qle = QtGui.QLineEdit(self) # 构造一个行编辑器        qle.move(60, 100)        self.lbl.move(60, 40)        qle.textChanged[str].connect(self.onChanged) # 将文本发生变化的信号和self.onChanged函数槽联系起来        self.setGeometry(300, 300, 280, 170)        self.setWindowTitle('QtGui.QLineEdit')        self.show()    def onChanged(self, text): # 自定义槽函数        self.lbl.setText(text) # 设置该窗体中lable的文本        self.lbl.adjustSize() # 自动调整lable的大小以适应文本的长度def main():    app = QtGui.QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())if __name__ == '__main__':    main()

三、QtGui.QSplitter分割板小工具,可以让我自动调节子窗口的大小,在该示例中我们用了3个frame 控件以及2个splitter分割板控件

# -*- coding: utf-8 -*-"""ZetCode PySide tutorialThis example showshow to use QtGui.QSplitter widget.author: Jan Bodnarwebsite: zetcode.comlast edited: August 2011"""import sysfrom PySide import QtGui, QtCoreclass Example(QtGui.QWidget):    def __init__(self):        super(Example, self).__init__()        self.initUI()    def initUI(self):        hbox = QtGui.QHBoxLayout(self)        topleft = QtGui.QFrame(self)        topleft.setFrameShape(QtGui.QFrame.StyledPanel)        topright = QtGui.QFrame(self)        topright.setFrameShape(QtGui.QFrame.StyledPanel)        bottom = QtGui.QFrame(self)        bottom.setFrameShape(QtGui.QFrame.StyledPanel)        splitter1 = QtGui.QSplitter(QtCore.Qt.Horizontal)        splitter1.addWidget(topleft)        splitter1.addWidget(topright)        splitter2 = QtGui.QSplitter(QtCore.Qt.Vertical)        splitter2.addWidget(splitter1)        splitter2.addWidget(bottom)        hbox.addWidget(splitter2)        self.setLayout(hbox)        QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('Cleanlooks'))        self.setGeometry(300, 300, 300, 200)        self.setWindowTitle('QtGui.QSplitter')        self.show()    def onChanged(self, text):        self.lbl.setText(text)        self.lbl.adjustSize()def main():    app = QtGui.QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())if __name__ == '__main__':    main()

四、组合框,下拉列表框

# -*- coding: utf-8 -*-"""ZetCode PySide tutorialThis example showshow to use QtGui.QComboBox widget.author: Jan Bodnarwebsite: zetcode.comlast edited: August 2011"""import sysfrom PySide import QtGui, QtCoreclass Example(QtGui.QWidget):    def __init__(self):        super(Example, self).__init__()        self.initUI()    def initUI(self):        self.lbl = QtGui.QLabel("Ubuntu", self)        combo = QtGui.QComboBox(self)        combo.addItem("Ubuntu")        combo.addItem("Mandriva")        combo.addItem("Fedora")        combo.addItem("Red Hat")        combo.addItem("Gentoo")        combo.move(50, 50)        self.lbl.move(50, 150)        combo.activated[str].connect(self.onActivated)        self.setGeometry(300, 300, 300, 200)        self.setWindowTitle('QtGui.QComboBox')        self.show()    def onActivated(self, text):        self.lbl.setText(text)        self.lbl.adjustSize()def main():    app = QtGui.QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())if __name__ == '__main__':    main()

0 0