[GUI in Python] PyQt5 -Widget-

Last time continued

Widgets I will summarize this site roughly in Japanese.

【Checkbox】

QCheckBox.py


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


import sys
from PyQt5.QtWidgets import QWidget, QCheckBox, QApplication
from PyQt5.QtCore import Qt


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

        #Chuck box creation
        cb = QCheckBox('Show title', self)
        cb.move(20, 20)
        #Check the check box
        cb.toggle()
        #Change the title depending on the check status
        cb.stateChanged.connect(self.changeTitle)
        
        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('QCheckBox')
        self.show()
        
        
    def changeTitle(self, state):
      
        #When the chuck is in
        if state == Qt.Checked:
            self.setWindowTitle('QCheckBox')
        #When the chuck is not inserted
        else:
            #It seems that the title will be python if you do not specify any arguments
            self.setWindowTitle('')
            
        
if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
checkbox.png

[Toggle button]

Toggle_button.py


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


import sys
from PyQt5.QtWidgets import (QWidget, QPushButton, 
    QFrame, QApplication)
from PyQt5.QtGui import QColor


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

        #Initial color setting (make it black)
        self.col = QColor(0, 0, 0)       

        #Creating a toggle button
        redb = QPushButton('Red', self)
        redb.setCheckable(True)
        redb.move(10, 10)

        #Call the setColor function when clicked
        redb.clicked[bool].connect(self.setColor)

        redb = QPushButton('Green', self)
        redb.setCheckable(True)
        redb.move(10, 60)

        redb.clicked[bool].connect(self.setColor)

        blueb = QPushButton('Blue', self)
        blueb.setCheckable(True)
        blueb.move(10, 110)

        blueb.clicked[bool].connect(self.setColor)

        self.square = QFrame(self)
        self.square.setGeometry(150, 20, 100, 100)
        self.square.setStyleSheet("QWidget { background-color: %s }" %  
            self.col.name())
        
        self.setGeometry(300, 300, 280, 170)
        self.setWindowTitle('Toggle button')
        self.show()
        
        
    def setColor(self, pressed):
        
        #Assign the pressed button to the source variable
        source = self.sender()      
        
        #Set the color when the button is clicked
        if pressed:
            val = 255
        else: val = 0
        
        #When the Red button is pressed, mix red with the color
        if source.text() == "Red":
            self.col.setRed(val)    
        #When the Green button is pressed, mix green with the color
        elif source.text() == "Green":
            self.col.setGreen(val)   
        #When the Blue button is pressed, mix blue with the color
        else:
            self.col.setBlue(val) 
        
        #Change color
        self.square.setStyleSheet("QFrame { background-color: %s }" %
            self.col.name())  
       
       
if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

initial state toggle_before.png

When you press the Red and Blue buttons toggle_after.png

【slider】

QSlider.py


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


import sys
from PyQt5.QtWidgets import (QWidget, QSlider, 
    QLabel, QApplication)
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap


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

        #Horizontal slider creation
        sld = QSlider(Qt.Horizontal, self)
        #Prevent the slider from being focused
        sld.setFocusPolicy(Qt.NoFocus)
        sld.setGeometry(30, 40, 100, 30)
        #The changeValue function is called when the slider moves
        sld.valueChanged[int].connect(self.changeValue)
        
        #Label creation
        self.label = QLabel(self)
        #Initial image setting for label
        self.label.setPixmap(QPixmap('orange.png'))
        self.label.setGeometry(160, 40, 80, 30)
        
        self.setGeometry(300, 300, 280, 170)
        self.setWindowTitle('QSlider')
        self.show()
        
        
    def changeValue(self, value):

        #Change the image depending on the position of the slider
        if value == 0:
            #Orange image
            self.label.setPixmap(QPixmap('orange.png'))
        elif value > 0 and value <= 30:
            #Yellow image
            self.label.setPixmap(QPixmap('yellow.png'))
        elif value > 30 and value < 80:
            #Green image
            self.label.setPixmap(QPixmap('green.png'))
        else:
            #Blue image
            self.label.setPixmap(QPixmap('blue.png'))
            

if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

orange_slider.png yellow_slider.png green_slider.png blue_slider.png

【progress bar】

QProgressBar.py


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


import sys
from PyQt5.QtWidgets import (QWidget, QProgressBar, 
    QPushButton, QApplication)
from PyQt5.QtCore import QBasicTimer


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

        #Creating a progress bar
        self.pbar = QProgressBar(self)
        self.pbar.setGeometry(30, 40, 200, 25)

        self.btn = QPushButton('Start', self)
        self.btn.move(40, 80)
        self.btn.clicked.connect(self.doAction)

        #Creating a timer object to move the progress bar
        self.timer = QBasicTimer()
        #Where to start the progress bar
        self.step = 0
        
        self.setGeometry(300, 300, 280, 170)
        self.setWindowTitle('QProgressBar')
        self.show()
        
    
    #Event handler for time
    def timerEvent(self, e):
      
        #100 progress bars%When that happens, stop the timer and set the button label to Finished.
        if self.step >= 100:
            self.timer.stop()
            self.btn.setText('Finished')
            return
            
        # 1%Increase the number little by little
        self.step = self.step + 1
        self.pbar.setValue(self.step)
        
    #What happens when the button is clicked
    def doAction(self):
      
        if self.timer.isActive():
            self.timer.stop()
            self.btn.setText('Start')
        else:
            #The first argument of start specifies the speed at which the bar advances
            self.timer.start(100, self)
            self.btn.setText('Stop')
            
        
if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

progress_1.png  progress_2.png  progress_3.png

【calendar】

QCalendarWidget.py


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


import sys
from PyQt5.QtWidgets import (QWidget, QCalendarWidget, 
    QLabel, QApplication)
from PyQt5.QtCore import QDate


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

        #Creating a calendar widget
        cal = QCalendarWidget(self)
        cal.setGridVisible(True)
        cal.move(20, 20)
        #Call the showDate function when a date is clicked
        cal.clicked[QDate].connect(self.showDate)
        
        self.lbl = QLabel(self)
        #Substitute today's date
        date = cal.selectedDate()
        #Make today's date label text
        self.lbl.setText(date.toString())
        #Move the label text to a position where it is easy to see
        self.lbl.move(430, 320)
        
        self.setGeometry(300, 300, 350, 300)
        self.setWindowTitle('Calendar')
        self.show()
        
        
    def showDate(self, date):     
        
        #Set the clicked date in the label text
        self.lbl.setText(date.toString())
        
        
if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

initial state calneder_1.png

Select June 15th calender_2.png

Next time will try Widgets II roughly.

Recommended Posts

[GUI in Python] PyQt5 -Widget-
Introducing GUI: PyQt5 in Python
[GUI in Python] PyQt5 -Event-
[GUI with Python] PyQt5 -Widget II-
[GUI with Python] PyQt5 -Custom Widget-
[GUI in Python] PyQt5-Dialog-
[GUI with Python] PyQt5 -Preparation-
[GUI with Python] PyQt5 -Paint-
GUI programming in Python using Appjar
[GUI in Python] PyQt5-Menu and Toolbar-
GUI creation in python using tkinter 2
Try to make it using GUI and PyQt in Python
Quadtree in Python --2
Python in optimization
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
GUI creation in python using tkinter part 1
Meta-analysis in Python
Unittest in python
Create a simple GUI app in Python
Epoch in Python
Discord in Python
Create Qt designer parts in Python (PyQt)
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
I made a GUI application with Python + PyQt5
GUI (WxPython) executable file (pyInstaller) [Windows] in Python3
Sorted list in Python
Daily AtCoder # 36 in Python