ZetCode PyQt4 tutorial widgets II

#!/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)
        # We create a QtGui.QPixmap object. It takes the name of the file as a parameter.
        pixmap = QtGui.QPixmap("redrock.png")

        lbl = QtGui.QLabel(self)
        # We put the pixmap into the QtGui.QLabel widget.
        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()    

--------------------------------------------------------------------------------

#!/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)
        # The QtGui.QLineEdit widget is created.
        qle = QtGui.QLineEdit(self)

        qle.move(60, 100)
        self.lbl.move(60, 40)

        # If the text in the line edit widget changes, we call the onChanged() method.
        qle.textChanged[str].connect(self.onChanged)

        self.setGeometry(300, 300, 280, 170)
        self.setWindowTitle('QtGui.QLineEdit')
        self.show()

    # Inside the onChanged() method, we set the typed text to the label widget. We call the adjustSize() method to adjust the size of the label to the length of the text.
    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()

--------------------------------------------------------------------------------

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

        # We use a styled frame in order to see the boundaries between the QtGui.QFrame widgets.
        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)

        # We create a QtGui.QSplitter widget and add two frames into it.
        splitter1 = QtGui.QSplitter(QtCore.Qt.Horizontal)
        splitter1.addWidget(topleft)
        splitter1.addWidget(topright)

        # We can also add a splitter to another splitter widget.
        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()    

--------------------------------------------------------------------------------

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

        # We create a QtGui.QComboBox widget with five options.
        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)

        # Upon an item selection, we call the onActivated() method.
        combo.activated[str].connect(self.onActivated)        

        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('QtGui.QComboBox')
        self.show()

    # Inside the method, we set the text of the chosen item to the label widget. We adjust the size of the label.
    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()

 

时间: 2024-10-13 00:44:41

ZetCode PyQt4 tutorial widgets II的相关文章

ZetCode PyQt4 tutorial layout management

!/usr/bin/python -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial This example shows three labels on a window using absolute positioning. author: Jan Bodnar website: zetcode.com last edited: October 2011 """ import sys from P

ZetCode PyQt4 tutorial work with menus, toolbars, a statusbar, and a main application window

!/usr/bin/python -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial This program creates a statusbar. author: Jan Bodnar website: zetcode.com last edited: September 2011 """ import sys from PyQt4 import QtGui class Example(QtGu

ZetCode PyQt4 tutorial signals and slots

#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, we connect a signal of a QtGui.QSlider to a slot of a QtGui.QLCDNumber. author: Jan Bodnar website: zetcode.com last edited: October 2011 ""&quo

ZetCode PyQt4 tutorial Dialogs

#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, we receive data from a QtGui.QInputDialog dialog. author: Jan Bodnar website: zetcode.com last edited: October 2011 """ import sys from PyQ

ZetCode PyQt4 tutorial custom widget

#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, we create a custom widget. author: Jan Bodnar website: zetcode.com last edited: October 2011 """ import sys from PyQt4 import QtGui, QtCore

ZetCode PyQt4 tutorial basic painting

#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, we draw text in Russian azbuka. author: Jan Bodnar website: zetcode.com last edited: September 2011 """ import sys from PyQt4 import QtGui,

ZetCode PyQt4 tutorial Drag and Drop

#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial This is a simple drag and drop example. author: Jan Bodnar website: zetcode.com last edited: January 2015 """ import sys from PyQt4 import QtGui # In order

PostgreSQL Python 开发者手册

标签 PostgreSQL , Java , php , ruby , python , C 背景 转自 http://zetcode.com/db/postgresqlpythontutorial/ 正文 This is a Python programming tutorial for the PostgreSQL database. It covers the basics of PostgreSQL programming with the Python language. You mi

PyQt4 Python GUI窗体应用程序

前言 还是一句老话,公司要什么我就做什么.这次来玩Python的GUI编程,做一个用于监控警报.简化SOP的窗体应用程序.首先当然是部署开发环境了,这次用PyCharm5+Python3.4+PyQt4(QT Designer) 软件环境 系统  Win10 软件  Python 3.4.3 PyCharm 5.0.4 PyQt 4 IPython 4.0.0 Powershell 3.0 PyQT简介 PyQt是一个创建GUI应用程序的工具包.它是Python编程语言和Qt库的成功融合.Qt库