Last time continued
Widgets I will summarize this site roughly in Japanese.
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_())
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
When you press the Red and Blue buttons
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_())
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_())
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
Select June 15th
Next time will try Widgets II roughly.
Recommended Posts