#!/usr/bin/env python3
from PyQt6.QtWidgets import (
QApplication, QWidget, QHBoxLayout, QVBoxLayout, QPushButton,
QFrame, QLabel, QSizePolicy, QSpacerItem
)
from PyQt6.QtGui import QPainter, QPaintEvent, QColor, QPen, QBrush, QMouseEvent, QFont
from PyQt6.QtCore import pyqtSignal
import sys
class BitButton(QWidget):
binaryStateChanged = pyqtSignal(int, bool)
def __init__(self):
super().__init__()
self.power: int = 0
self.state: bool = False
self.setFixedSize(24, 24)
def mouseReleaseEvent(self, a0):
self.setBinaryState(not self.state)
def setBinaryState(self, newState: bool):
self.state = newState
self.update()
def setFromBase10(self, withinValue: int):
self.setBinaryState(False if withinValue & 2**self.power == 0 else True)
def paintEvent(self, pe: QPaintEvent):
pen_color: QColor = QColor("#30aa53") if self.state else QColor("#204023")
brush_color: QColor = QColor("#78ffa3") if self.state else QColor("#258022")
valueChar: str = "1" if self.state else "0"
p: QPainter = QPainter(self)
p.setPen(QPen(pen_color, 3))
p.setBrush(brush_color)
p.drawRoundedRect(self.rect().toRectF(), 4.0, 4.0)
p.setFont(QFont("FreeMono", 12, QFont.Weight.DemiBold))
offset: QPoint = p.fontMetrics().boundingRectChar(valueChar).center()
p.drawText(self.rect().center() - offset, valueChar)
class ByteBox(QWidget):
def __init__(self):
super().__init__()
self.setContentsMargins(5, 3, 5, 3)
self.build_ui()
def build_ui(self):
hLayout: QHBoxLayout = QHBoxLayout()
hLayout.setContentsMargins(2, 2, 2, 2)
self.sizePolicy().setHorizontalPolicy(QSizePolicy.Policy.Minimum)
self.sizePolicy().setVerticalPolicy(QSizePolicy.Policy.Minimum)
for b in range(8):
bit: Bitton = BitButton()
bit.power = b
hLayout.addWidget(bit)
if b == 3: hLayout.addSpacing(6)
self.setLayout(hLayout)
def paintEvent(self, a0):
bg_color: QColor = QColor()
bg_color.setAlpha(0)
p: QPainter = QPainter(self)
p.setPen(QPen(QColor("#ffffff"), 3))
p.setBrush(bg_color)
p.drawRoundedRect(self.rect().toRectF(), 4.0, 4.0)
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Nonbinary, the Decimal/Binary/Hex Calculator")
self.resize(400, 400)
self.build_ui()
self.grandValue = 0
def build_ui(self):
vBoxLayout: QVBoxLayout = QVBoxLayout()
for r in range(2):
row: QHBoxLayout = QHBoxLayout()
for bb in range(4):
byteBox: ByteBox = ByteBox()
row.addWidget(byteBox)
vBoxLayout.addLayout(row)
self.setLayout(vBoxLayout)
def main():
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec())
if __name__ == "__main__":
main()
Comments
0 B
|0 👍
/0 👎
0 B
|👍
/👎