• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆 - 41, 文章 - 8, 評(píng)論 - 8, 引用 - 0
            數(shù)據(jù)加載中……

            [Python][PyQt4]PyQt4 Widget II

            PyQt4 Widget II

            這里我們將繼續(xù)介紹 PyQt4 組件。我們將涉及 QtGui.QPixmapQtGui.QLineEditQtGui.QSplitterQtGui.QComboBox

            QtGui.QPixmap

            QtGui.QPixmap 是可以處理圖片的組件之一。它對(duì)顯示圖片進(jìn)行了優(yōu)化。在我們的例子中,我們會(huì)用 QtGui.QPixmap 來(lái)顯示圖片。

            #!/usr/bin/python
            # -*- coding: utf-8 -*-
            """
            ZetCode PyQt4 tutorial
            In this example, we dispay an image
            on the window.
            author: Jan Bodnar
            website: zetcode.com
            last edited: September 2011
            """
            import sys
            from PyQt4 import QtGui, QtCore
            class Example(QtGui.QWidget):
                def __init__(self):
                    super(Example, self).__init__()
                    self.initUI()
                def initUI(self):
                    hbox = QtGui.QHBoxLayout(self)
                    pixmap = QtGui.QPixmap("redrock.png")
                    lbl = QtGui.QLabel(self)
                    lbl.setPixmap(pixmap)
                    hbox.addWidget(lbl)
                    self.setLayout(hbox)
                    self.move(300, 200)
                    self.setWindowTitle('Red Rock')
                    self.show()
            def main():
                app = QtGui.QApplication(sys.argv)
                ex = Example()
                sys.exit(app.exec_())
            if __name__ == '__main__':
                main()
            

            在這個(gè)例子里,我們顯示了一幅圖片。

            pixmap = QtGui.QPixmap("redrock.png")
            

            我們創(chuàng)建了一個(gè) QtGui.QPixmap 對(duì)象。它接受文件名作為參數(shù)。

            lbl = QtGui.QLabel(self)
            lbl.setPixmap(pixmap)
            

            我們把 pixmap 放到了 QtGui.QLabel 中。

            QtGui.QLineEdit

            QtGui.QLineEdit 是一個(gè)允許輸入和編輯一行純文本。這個(gè)組件中可以撤銷/恢復(fù),剪切/粘貼以及拖拉。

            #!/usr/bin/python
            # -*- coding: utf-8 -*-
            """
            ZetCode PyQt4 tutorial
            This example shows text which
            is entered in a QtGui.QLineEdit
            in a QtGui.QLabel widget.
            author: Jan Bodnar
            website: zetcode.com
            last edited: August 2011
            """
            import sys
            from PyQt4 import QtGui, QtCore
            class Example(QtGui.QWidget):
                def __init__(self):
                    super(Example, self).__init__()
                    self.initUI()
                def initUI(self):
                    self.lbl = QtGui.QLabel(self)
                    qle = QtGui.QLineEdit(self)
                    qle.move(60, 100)
                    self.lbl.move(60, 40)
                    qle.textChanged[str].connect(self.onChanged)
                    self.setGeometry(300, 300, 280, 170)
                    self.setWindowTitle('QtGui.QLineEdit')
                    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()
            

            這個(gè)例子顯示一個(gè)行編輯區(qū)和一個(gè)標(biāo)簽。我們輸入的就會(huì)立即在標(biāo)簽中顯示。

            qle = QtGui.QLineEdit(self)
            

            創(chuàng)建了 QtGui.QLineEdit 組件。

            qle.textChanged[str].connect(self.onChanged)
            

            如果文本區(qū)的文本改變了,我們就調(diào)用 onChanged() 方法。

            def onChanged(self, text):
                self.lbl.setText(text)
                self.lbl.adjustSize()
            

            onChanged() 方法中,我們把輸入的文本放到了標(biāo)簽中。通過調(diào)用 adjustSize() 方法,我們把標(biāo)簽設(shè)置到文本的長(zhǎng)度。

            QtGui.QSplitter

            QtGui.QSplitter 可以讓用戶控制子組件的大小,通過拖動(dòng)子組件的大小。在我們的例子中,我們的三個(gè) QtGui.QFrame 將由兩個(gè) splitter 分割。

            #!/usr/bin/python
            # -*- coding: utf-8 -*-
            """
            ZetCode PyQt4 tutorial
            This example shows
            how to use QtGui.QSplitter widget.
            author: Jan Bodnar
            website: zetcode.com
            last edited: September 2011
            """
            import sys
            from PyQt4 import QtGui, QtCore
            class 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 main():
                app = QtGui.QApplication(sys.argv)
                ex = Example()
                sys.exit(app.exec_())
            if __name__ == '__main__':
                main()
            

            在這個(gè)例子中,有三個(gè)框架組件,兩個(gè)分割條。

            topleft = QtGui.QFrame(self)
            topleft.setFrameShape(QtGui.QFrame.StyledPanel)
            

            我們使用了有樣式的框架,主要用于看到邊框。

            splitter1 = QtGui.QSplitter(QtCore.Qt.Horizontal)
            splitter1.addWidget(topleft)
            splitter1.addWidget(topright)
            

            我們創(chuàng)建了一個(gè) QtGui.QSplitter 組件,并添加到兩個(gè)框架。

            splitter2 = QtGui.QSplitter(QtCore.Qt.Vertical)
            splitter2.addWidget(splitter1)
            

            我們也可以把一個(gè) splitter 添加到 splitter 中。

            QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('Cleanlooks'))
            

            我們使用一個(gè)簡(jiǎn)潔的樣式。在有些樣式中,框架是不可見的。

            QtGui.QComboBox

            QtGui.QComboBox 允許用戶從一組選項(xiàng)中選取一個(gè)。

            #!/usr/bin/python
            # -*- coding: utf-8 -*-
            """
            ZetCode PyQt4 tutorial
            This example shows
            how to use QtGui.QComboBox widget.
            author: Jan Bodnar
            website: zetcode.com
            last edited: September 2011
            """
            import sys
            from PyQt4 import QtGui, QtCore
            class 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()
            

            這個(gè)例子中有一個(gè) QtGui.QComboBoxQtGui.QLable 。這里有五個(gè)選項(xiàng)。它們是 Linux 的發(fā)行版。標(biāo)簽中就會(huì)顯示選中的項(xiàng)目。

            combo = QtGui.QComboBox(self)
            combo.addItem("Ubuntu")
            combo.addItem("Mandriva")
            combo.addItem("Fedora")
            combo.addItem("Red Hat")
            combo.addItem("Gentoo")
            

            我們創(chuàng)建一個(gè) QtGui.QComboBox 組件并增加了五個(gè)選項(xiàng)。

            combo.activated[str].connect(self.onActivated)
            

            當(dāng)選擇一個(gè)選項(xiàng)后,我們調(diào)用了 onActivated() 方法。

            def onActivated(self, text):
                self.lbl.setText(text)
                self.lbl.adjustSize()
            

            在這個(gè)方法中,我們把選中的選項(xiàng)的文本放到標(biāo)簽中。我們還調(diào)整標(biāo)簽的大小。


            在本部分,我們涉及了另外四個(gè) PyQt4 組件。

            posted on 2012-02-12 10:13 mirguest 閱讀(1918) 評(píng)論(0)  編輯 收藏 引用 所屬分類: Python

            国产精品女同一区二区久久| 99麻豆久久久国产精品免费| 久久精品中文字幕第23页| 精品国产婷婷久久久| 久久这里有精品视频| 亚洲国产天堂久久综合| 人妻精品久久久久中文字幕一冢本| 久久99精品国产麻豆宅宅| 日本强好片久久久久久AAA| 日本福利片国产午夜久久| 日批日出水久久亚洲精品tv| 久久久久人妻精品一区二区三区| 久久综合久久综合久久| 久久久久久午夜精品| 国产精品久久免费| 久久妇女高潮几次MBA| 国内精品久久久久久久久| 久久久久亚洲AV成人片| 亚洲国产日韩欧美综合久久| 热久久国产精品| 久久99精品久久久久久久久久| 伊人情人综合成人久久网小说| 久久综合九色综合久99 | 国内精品久久久久影院免费| 久久中文精品无码中文字幕| 99国产精品久久| 狼狼综合久久久久综合网| 亚洲国产成人久久精品99| 久久久久四虎国产精品| 久久久久久毛片免费播放| 久久人与动人物a级毛片| 久久精品无码一区二区app| 日韩欧美亚洲综合久久影院d3| 久久久久亚洲AV无码永不| 午夜精品久久久久久99热| 久久久久久国产精品美女| yy6080久久| 伊人久久综合无码成人网| 久久精品国产亚洲AV忘忧草18| 欧美久久天天综合香蕉伊| 伊人久久大香线蕉精品|