• <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, 評論 - 8, 引用 - 0
            數據加載中……

            [Python][PyQt4]PyQt4 Widget II

            PyQt4 Widget II

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

            QtGui.QPixmap

            QtGui.QPixmap 是可以處理圖片的組件之一。它對顯示圖片進行了優化。在我們的例子中,我們會用 QtGui.QPixmap 來顯示圖片。

            #!/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()
            

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

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

            我們創建了一個 QtGui.QPixmap 對象。它接受文件名作為參數。

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

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

            QtGui.QLineEdit

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

            #!/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()
            

            這個例子顯示一個行編輯區和一個標簽。我們輸入的就會立即在標簽中顯示。

            qle = QtGui.QLineEdit(self)
            

            創建了 QtGui.QLineEdit 組件。

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

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

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

            onChanged() 方法中,我們把輸入的文本放到了標簽中。通過調用 adjustSize() 方法,我們把標簽設置到文本的長度。

            QtGui.QSplitter

            QtGui.QSplitter 可以讓用戶控制子組件的大小,通過拖動子組件的大小。在我們的例子中,我們的三個 QtGui.QFrame 將由兩個 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()
            

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

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

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

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

            我們創建了一個 QtGui.QSplitter 組件,并添加到兩個框架。

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

            我們也可以把一個 splitter 添加到 splitter 中。

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

            我們使用一個簡潔的樣式。在有些樣式中,框架是不可見的。

            QtGui.QComboBox

            QtGui.QComboBox 允許用戶從一組選項中選取一個。

            #!/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()
            

            這個例子中有一個 QtGui.QComboBoxQtGui.QLable 。這里有五個選項。它們是 Linux 的發行版。標簽中就會顯示選中的項目。

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

            我們創建一個 QtGui.QComboBox 組件并增加了五個選項。

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

            當選擇一個選項后,我們調用了 onActivated() 方法。

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

            在這個方法中,我們把選中的選項的文本放到標簽中。我們還調整標簽的大小。


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

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

            成人国内精品久久久久影院VR| 狠狠色丁香久久婷婷综合_中| 精品国产一区二区三区久久久狼 | 久久99国产综合精品| 人妻无码久久一区二区三区免费| 久久国产精品无码一区二区三区 | 久久精品国产只有精品2020| 久久国产热这里只有精品| 久久婷婷五月综合国产尤物app | 久久久久亚洲AV综合波多野结衣 | 久久亚洲国产成人影院网站| 浪潮AV色综合久久天堂| 精品无码人妻久久久久久| 日产精品久久久久久久性色| 久久久久一级精品亚洲国产成人综合AV区| 久久精品卫校国产小美女| 久久99亚洲综合精品首页| 无码人妻久久一区二区三区免费 | 国产真实乱对白精彩久久| 一本久久a久久精品亚洲| 久久国产精品偷99| 好久久免费视频高清| 久久午夜羞羞影院免费观看| 久久精品国产清自在天天线| 久久er国产精品免费观看8| 99re这里只有精品热久久| 久久久久久曰本AV免费免费| 婷婷久久五月天| 久久精品国产黑森林| 久久精品国产99久久香蕉| 久久综合久久综合久久| 久久国产精品-久久精品| 99久久综合狠狠综合久久止| 久久久久亚洲精品无码蜜桃| 久久久久久九九99精品| 久久久久亚洲Av无码专| 国产精品久久久福利| 日本道色综合久久影院| 一本久久a久久精品综合夜夜| 久久福利青草精品资源站免费| 久久国产免费观看精品|