ZetCode PyQt4 tutorial basic painting

简介: #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, we draw text in Russian azbuka.
#!/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, QtCore

class Example(QtGui.QWidget):
    
    def __init__(self):
        super(Example, self).__init__()
        
        self.initUI()
        
    def initUI(self):      

        self.text = u'\u041b\u0435\u0432 \u041d\u0438\u043a\u043e\u043b\u0430\
\u0435\u0432\u0438\u0447 \u0422\u043e\u043b\u0441\u0442\u043e\u0439: \n\
\u0410\u043d\u043d\u0430 \u041a\u0430\u0440\u0435\u043d\u0438\u043d\u0430'

        self.setGeometry(300, 300, 280, 170)
        self.setWindowTitle('Draw text')
        self.show()

    # Drawing is done within the paint event.
    def paintEvent(self, event):

        # The QtGui.QPainter class is responsible for all the low-level painting. All the painting methods go between begin() and end() methods. The actual painting is delegated to the drawText() method.
        qp = QtGui.QPainter()
        qp.begin(self)
        self.drawText(event, qp)
        qp.end()
        
    def drawText(self, event, qp):
      
        qp.setPen(QtGui.QColor(168, 34, 3))
        qp.setFont(QtGui.QFont('Decorative', 10))
        # The drawText() method draws text on the window. The rect() method of the paint event returns the rectangle that needs to be updated.
        qp.drawText(event.rect(), QtCore.Qt.AlignCenter, self.text)        
                
        
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 

In the example, we draw randomly 1000 red points 
on the window.

author: Jan Bodnar
website: zetcode.com 
last edited: September 2011
"""

import sys, random
from PyQt4 import QtGui, QtCore

class Example(QtGui.QWidget):
    
    def __init__(self):
        super(Example, self).__init__()
        
        self.initUI()
        
    def initUI(self):      

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

    def paintEvent(self, e):

        qp = QtGui.QPainter()
        qp.begin(self)
        self.drawPoints(qp)
        qp.end()
        
    def drawPoints(self, qp):
      
        # We set the pen to red colour. We use a predefined QtCore.Qt.red colour constant.
        qp.setPen(QtCore.Qt.red)
        # Each time we resize the window, a paint event is generated. We get the current size of the window with the size() method. We use the size of the window to distribute the points all over the client area of the window.
        size = self.size()
        
        for i in range(1000):
            x = random.randint(1, size.width()-1)
            y = random.randint(1, size.height()-1)
            # We draw the point with the drawPoint() method.
            qp.drawPoint(x, y)     
                
        
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 draws three rectangles in three
different colours. 

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.setGeometry(300, 300, 350, 100)
        self.setWindowTitle('Colours')
        self.show()

    def paintEvent(self, e):

        qp = QtGui.QPainter()
        qp.begin(self)
        self.drawRectangles(qp)
        qp.end()
        
    def drawRectangles(self, qp):
      
        # Here we define a colour using a hexadecimal notation.
        color = QtGui.QColor(0, 0, 0)
        color.setNamedColor('#d4d4d4')
        qp.setPen(color)

        # Here we define a brush and draw a rectangle. A brush is an elementary graphics object which is used to draw the background of a shape. The drawRect() method accepts four parameters. The first two are x and y values on the axis. The third and fourth parameters are the width and height of the rectangle. The method draws the rectangle using the current pen and brush.
        qp.setBrush(QtGui.QColor(200, 0, 0))
        qp.drawRect(10, 15, 90, 60)

        qp.setBrush(QtGui.QColor(255, 80, 0, 160))
        qp.drawRect(130, 15, 90, 60)

        qp.setBrush(QtGui.QColor(25, 0, 90, 200))
        qp.drawRect(250, 15, 90, 60)
              
        
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 

In this example we draw 6 lines using
different pen styles. 

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.setGeometry(300, 300, 280, 270)
        self.setWindowTitle('Pen styles')
        self.show()

    def paintEvent(self, e):

        qp = QtGui.QPainter()
        qp.begin(self)
        self.drawLines(qp)
        qp.end()
        
    def drawLines(self, qp):
      
        # We create a QtGui.QPen object. The colour is black. The width is set to 2 pixels so that we can see the differences between the pen styles. The QtCore.Qt.SolidLine is one of the predefined pen styles.
        pen = QtGui.QPen(QtCore.Qt.black, 2, QtCore.Qt.SolidLine)

        qp.setPen(pen)
        qp.drawLine(20, 40, 250, 40)

        # Here we define a custom pen style. We set a QtCore.Qt.CustomDashLine pen style and call the setDashPattern() method. The list of numbers defines a style. There must be an even number of numbers. Odd numbers define a dash, even numbers space. The greater the number, the greater the space or the dash. Our pattern is 1px dash, 4px space, 5px dash, 4px space etc.
        pen.setStyle(QtCore.Qt.DashLine)
        qp.setPen(pen)
        qp.drawLine(20, 80, 250, 80)

        pen.setStyle(QtCore.Qt.DashDotLine)
        qp.setPen(pen)
        qp.drawLine(20, 120, 250, 120)

        pen.setStyle(QtCore.Qt.DotLine)
        qp.setPen(pen)
        qp.drawLine(20, 160, 250, 160)

        pen.setStyle(QtCore.Qt.DashDotDotLine)
        qp.setPen(pen)
        qp.drawLine(20, 200, 250, 200)

        pen.setStyle(QtCore.Qt.CustomDashLine)
        pen.setDashPattern([1, 4, 5, 4])
        qp.setPen(pen)
        qp.drawLine(20, 240, 250, 240)
              
        
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 draws 9 rectangles in different
brush styles.

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.setGeometry(300, 300, 355, 280)
        self.setWindowTitle('Brushes')
        self.show()

    def paintEvent(self, e):

        qp = QtGui.QPainter()
        qp.begin(self)
        self.drawBrushes(qp)
        qp.end()
        
    def drawBrushes(self, qp):
      
        # We define a brush object. We set it to the painter object and draw the rectangle by calling the drawRect() method.
        brush = QtGui.QBrush(QtCore.Qt.SolidPattern)
        qp.setBrush(brush)
        qp.drawRect(10, 15, 90, 60)

        brush.setStyle(QtCore.Qt.Dense1Pattern)
        qp.setBrush(brush)
        qp.drawRect(130, 15, 90, 60)

        brush.setStyle(QtCore.Qt.Dense2Pattern)
        qp.setBrush(brush)
        qp.drawRect(250, 15, 90, 60)

        brush.setStyle(QtCore.Qt.Dense3Pattern)
        qp.setBrush(brush)
        qp.drawRect(10, 105, 90, 60)

        brush.setStyle(QtCore.Qt.DiagCrossPattern)
        qp.setBrush(brush)
        qp.drawRect(10, 105, 90, 60)

        brush.setStyle(QtCore.Qt.Dense5Pattern)
        qp.setBrush(brush)
        qp.drawRect(130, 105, 90, 60)

        brush.setStyle(QtCore.Qt.Dense6Pattern)
        qp.setBrush(brush)
        qp.drawRect(250, 105, 90, 60)

        brush.setStyle(QtCore.Qt.HorPattern)
        qp.setBrush(brush)
        qp.drawRect(10, 195, 90, 60)

        brush.setStyle(QtCore.Qt.VerPattern)
        qp.setBrush(brush)
        qp.drawRect(130, 195, 90, 60)

        brush.setStyle(QtCore.Qt.BDiagPattern)
        qp.setBrush(brush)
        qp.drawRect(250, 195, 90, 60)
              
        
def main():
    
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

 

目录
相关文章
|
4月前
|
JSON API 数据安全/隐私保护
深度分析淘宝卖家订单详情API接口,用json返回数据
淘宝卖家订单详情API(taobao.trade.fullinfo.get)是淘宝开放平台提供的重要接口,用于获取单个订单的完整信息,包括订单状态、买家信息、商品明细、支付与物流信息等,支撑订单管理、ERP对接及售后处理。需通过appkey、appsecret和session认证,并遵守调用频率与数据权限限制。本文详解其使用方法并附Python调用示例。
|
4月前
|
缓存 Windows
错误代码0x8007000d处理方法?
错误代码0x8007000d通常与系统文件损坏、安装介质问题或更新文件缺失相关,以下是综合解决方案:
|
5月前
|
数据采集 数据挖掘 BI
Dataphin功能Tips系列(67)如何将BI报表纳入资产管理
Dataphin通过采集BI报表元数据,实现报表资产的信息完善与上架管理,助力企业构建统一的数据资产门户。以QuickBI为例,介绍如何配置应用系统、创建采集任务,并实现报表资产的统一管理与跳转分析。
|
4月前
|
人工智能 运维 监控
云+应用一体化可观测:破局“云上困境”,让运维驱动业务增长
当云计算迈入深入上云新阶段,数智化升级的关键课题已从“简单上云”演进至“精细治云”。随着企业对云计算的依赖日益加深,如何高效管理云端资源及其稳定性成为新的挑战。为此,阿里云推出云+应用一体化可观测方案,通过阿里云应用运维平台(Application Operation Platform,简称“AOP”)构建覆盖应用全生命周期一体化可观测产品体系,推动运维模式由被动响应向主动预防转变,实现故障的快速发现、定界与恢复,保障云上业务稳定运行。 目前,该方案已成功服务超过50家行业头部客户,为政务云平台、金融核心系统、能源调度中枢等关键基础设施提供全天候安全运维保障。
237 0
|
7月前
|
存储 分布式计算 供应链
Dataphin功能Tips系列(51)-支持增全量一体实时集成
本文介绍了基于增全量一体实时集成的库存管理与分析解决方案。通过将业务中台的库存表同步至MaxCompute Delta表,实现离线与实时分析的统一支持。相比传统方案,该方法确保数据一致性,优化存储成本,降低维护复杂度,并大幅提升实时性,满足高效库存管理需求。
149 5
|
8月前
|
大数据 数据处理 数据安全/隐私保护
数据治理,你真的合规了吗?——从代码到实践的深度解析
数据治理,你真的合规了吗?——从代码到实践的深度解析
234 8
|
8月前
|
前端开发 Java PHP
开发体育赛事直播系统:实现聊天交友的私聊功能技术实现全方案解析
本文基于体育赛事直播系统,详细介绍了用户间私聊功能的完整实现方案。技术栈涵盖后端(PHP ThinkPHP)、前端(Vue.js)、移动端(Android Java、iOS OC),并结合MySQL数据库与WebSocket+Redis实现实时通信。功能包括一对一私聊、聊天记录显示、未读消息提示、消息免打扰、聊天置顶、删除/清空聊天记录等。文章提供了数据结构设计、接口代码示例及前后端关键实现细节,适合开发者学习参考。
|
11月前
|
网络协议 应用服务中间件 网络安全
免费IP地址SSL证书在哪里申请
免费IP地址SSL证书的申请需通过特定的证书颁发机构(CA)或平台。JoySSL提供针对IP地址的免费SSL证书,Let's Encrypt则主要面向网站。申请步骤包括:访问官网注册账号(JoySSL需填写注册码230922),选择证书类型,填写信息并验证IP地址所有权,提交审核,下载部署证书。注意事项:确保IP地址有效、服务器支持HTTPS,并定期续签证书以保持有效性。
|
11月前
|
供应链 安全 JavaScript
开源社区漏洞治理策略与实践
本次分享的主题是开源社区漏洞治理策略与实践,由安势信息的高坤分享。主要分为四个部分: 1. 为什么要重视软件供应链安全时变量池分享 2. 我们可借鉴的国外优秀探索 3. SBOM的挑战 4. 安势的探索及成果
264 2
|
SQL 分布式计算 资源调度
常用大数据组件的Web端口号总结
这是关于常用大数据组件Web端口号的总结。通过虚拟机名+端口号可访问各组件服务:Hadoop HDFS的9870,YARN的ResourceManager的8088和JobHistoryServer的19888,Zeppelin的8000,HBase的10610,Hive的10002。ZooKeeper的端口包括客户端连接的2181,服务器间通信的2888以及选举通信的3888。
540 2
常用大数据组件的Web端口号总结