[GUI with Python] PyQt5 -Custom Widget-

Continuation of Last time

Custom widgets I will summarize this site roughly in Japanese.

[Burning widget]

Burning_widget.py


#!/usr/bin/python3
# -*- coding: utf-8 -*-


import sys
from PyQt5.QtWidgets import (QWidget, QSlider, QApplication, 
    QHBoxLayout, QVBoxLayout)
from PyQt5.QtCore import QObject, Qt, pyqtSignal
from PyQt5.QtGui import QPainter, QFont, QColor, QPen


class Communicate(QObject):
    
    updateBW = pyqtSignal(int)


class BurningWidget(QWidget):
  
    def __init__(self):      
        super().__init__()
        
        self.initUI()
        
        
    def initUI(self):
        
        #Set minimum size for burning widget
        self.setMinimumSize(1, 30)
        #Set the position where the color arrives
        self.value = 75
        #Scale setting
        self.num = [75, 150, 225, 300, 375, 450, 525, 600, 675]


    def setValue(self, value):

        self.value = value


    def paintEvent(self, e):
      
        qp = QPainter()
        qp.begin(self)
        self.drawWidget(qp)
        qp.end()
      
      
    def drawWidget(self, qp):
      
        #Font settings
        font = QFont('Serif', 7, QFont.Light)
        qp.setFont(font)

        #Burning widget sizing
        size = self.size()
        w = size.width()
        h = size.height()

        step = int(round(w / 10.0))

        #The entire area to be colored
        till = int(((w / 750.0) * self.value))
        #Area colored in red
        full = int(((w / 750.0) * 700))

        #Color setting for areas colored in red
        if self.value >= 700:
            
            qp.setPen(QColor(255, 255, 255))
            qp.setBrush(QColor(255, 255, 184))
            qp.drawRect(0, 0, full, h)
            qp.setPen(QColor(255, 175, 175))
            qp.setBrush(QColor(255, 175, 175))
            qp.drawRect(full, 0, till-full, h)
            
        #Color settings other than the area colored in red
        else:
            
            qp.setPen(QColor(255, 255, 255))
            qp.setBrush(QColor(255, 255, 184))
            qp.drawRect(0, 0, till, h)

        #Burning widget background
        pen = QPen(QColor(20, 20, 20), 1, 
            Qt.SolidLine)
            
        qp.setPen(pen)
        qp.setBrush(Qt.NoBrush)
        qp.drawRect(0, 0, w-1, h-1)

        j = 0

        #Write a scale
        for i in range(step, 10*step, step):
          
            qp.drawLine(i, 0, i, 5)
            metrics = qp.fontMetrics()
            fw = metrics.width(str(self.num[j]))
            qp.drawText(i-fw/2, h/2, str(self.num[j]))
            j = j + 1
            

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

        sld = QSlider(Qt.Horizontal, self)
        sld.setFocusPolicy(Qt.NoFocus)
        sld.setRange(1, 750)
        sld.setValue(75)
        sld.setGeometry(30, 40, 150, 30)

        self.c = Communicate()        
        self.wid = BurningWidget()
        self.c.updateBW[int].connect(self.wid.setValue)

        #Calling the changeValue function when moving the slider
        sld.valueChanged[int].connect(self.changeValue)
        hbox = QHBoxLayout()
        hbox.addWidget(self.wid)
        vbox = QVBoxLayout()
        vbox.addStretch(1)
        vbox.addLayout(hbox)
        self.setLayout(vbox)
        
        self.setGeometry(300, 300, 390, 210)
        self.setWindowTitle('Burning widget')
        self.show()
        
        
    def changeValue(self, value):
             
        #Send the current value of the slider
        self.c.updateBW.emit(value)        
        self.wid.repaint()
        
        
if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
burning_widget.png

Recommended Posts

[GUI with Python] PyQt5 -Custom Widget-
[GUI with Python] PyQt5 -Widget II-
[GUI in Python] PyQt5 -Widget-
[GUI with Python] PyQt5 -Preparation-
[GUI with Python] PyQt5 -Paint-
I made a GUI application with Python + PyQt5
Introducing GUI: PyQt5 in Python
[GUI in Python] PyQt5 -Event-
[GUI with Python] PyQt5-The first step-
[GUI with Python] PyQt5-Drag and drop-
Utilize Python custom scripts with StackStorm
I played with PyQt5 and Python3
[Azure] Hit Custom Vision Service with Python
Happy GUI construction with electron and python
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Play with 2016-Python
Tested with Python
with syntax (Python)
Bingo with python
Excel with Python
Microcomputer with Python
Cast with python
GUI image cropping tool made with Python + Tkinter
GUI automation with Python x Windows App Driver
Create a color-specified widget with Python + Qt (PySide)
Simple sales tool creation with Python GUI: Quote creation
Open a file dialog with a python GUI (tkinter.filedialog)
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
1.1 Getting Started with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
Kernel Method with Python
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS
Posting tweets with python
Drive WebDriver with python
Use mecab with Python3
Summary of tools for operating Windows GUI with Python