pastabowl

Untitled

Oct 24th, 2025 (edited)
1,680
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.08 KB | Source Code | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. from PyQt6.QtWidgets import (
  4.     QApplication, QWidget, QHBoxLayout, QVBoxLayout, QPushButton,
  5.     QFrame, QLabel, QSizePolicy, QSpacerItem
  6. )
  7. from PyQt6.QtGui import QPainter, QPaintEvent, QColor, QPen, QBrush, QMouseEvent, QFont
  8. from PyQt6.QtCore import pyqtSignal
  9. import sys
  10.  
  11.  
  12. class BitButton(QWidget):
  13.     binaryStateChanged = pyqtSignal(int, bool)
  14.  
  15.     def __init__(self):
  16.         super().__init__()
  17.         self.power: int = 0
  18.         self.state: bool = False
  19.         self.setFixedSize(24, 24)
  20.  
  21.     def mouseReleaseEvent(self, a0):
  22.         self.setBinaryState(not self.state)
  23.  
  24.     def setBinaryState(self, newState: bool):
  25.         self.state = newState
  26.         self.update()
  27.  
  28.     def setFromBase10(self, withinValue: int):
  29.         self.setBinaryState(False if withinValue & 2**self.power == 0 else True)
  30.  
  31.     def paintEvent(self, pe: QPaintEvent):
  32.         pen_color: QColor = QColor("#30aa53") if self.state else QColor("#204023")
  33.         brush_color: QColor = QColor("#78ffa3") if self.state else QColor("#258022")
  34.         valueChar: str = "1" if self.state else "0"
  35.         p: QPainter = QPainter(self)
  36.         p.setPen(QPen(pen_color, 3))
  37.         p.setBrush(brush_color)
  38.         p.drawRoundedRect(self.rect().toRectF(), 4.0, 4.0)
  39.         p.setFont(QFont("FreeMono", 12, QFont.Weight.DemiBold))
  40.         offset: QPoint = p.fontMetrics().boundingRectChar(valueChar).center()
  41.         p.drawText(self.rect().center() - offset, valueChar)
  42.  
  43. class ByteBox(QWidget):
  44.     def __init__(self):
  45.         super().__init__()
  46.         self.setContentsMargins(5, 3, 5, 3)
  47.         self.build_ui()
  48.  
  49.     def build_ui(self):
  50.         hLayout: QHBoxLayout = QHBoxLayout()
  51.         hLayout.setContentsMargins(2, 2, 2, 2)
  52.         self.sizePolicy().setHorizontalPolicy(QSizePolicy.Policy.Minimum)
  53.         self.sizePolicy().setVerticalPolicy(QSizePolicy.Policy.Minimum)
  54.         for b in range(8):
  55.             bit: Bitton = BitButton()
  56.             bit.power = b
  57.             hLayout.addWidget(bit)
  58.             if b == 3: hLayout.addSpacing(6)
  59.         self.setLayout(hLayout)
  60.  
  61.     def paintEvent(self, a0):
  62.         bg_color: QColor = QColor()
  63.         bg_color.setAlpha(0)
  64.         p: QPainter = QPainter(self)
  65.         p.setPen(QPen(QColor("#ffffff"), 3))
  66.         p.setBrush(bg_color)
  67.         p.drawRoundedRect(self.rect().toRectF(), 4.0, 4.0)
  68.  
  69.  
  70. class MainWindow(QWidget):
  71.     def __init__(self):
  72.         super().__init__()
  73.         self.setWindowTitle("Nonbinary, the Decimal/Binary/Hex Calculator")
  74.         self.resize(400, 400)
  75.         self.build_ui()
  76.  
  77.         self.grandValue = 0
  78.  
  79.     def build_ui(self):
  80.         vBoxLayout: QVBoxLayout = QVBoxLayout()
  81.         for r in range(2):
  82.             row: QHBoxLayout = QHBoxLayout()
  83.             for bb in range(4):
  84.                 byteBox: ByteBox = ByteBox()
  85.                 row.addWidget(byteBox)
  86.             vBoxLayout.addLayout(row)
  87.         self.setLayout(vBoxLayout)
  88.  
  89. def main():
  90.     app = QApplication(sys.argv)
  91.     w = MainWindow()
  92.     w.show()
  93.     sys.exit(app.exec())
  94.  
  95.  
  96. if __name__ == "__main__":
  97.     main()
  98.  
  99.  
Advertisement