Digitale bierlijst

gui.py 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. """
  2. Provides the graphical front-end for Piket.
  3. """
  4. import sys
  5. from PySide2.QtWidgets import QApplication, QPushButton, QGridLayout, QWidget
  6. from PySide2.QtGui import QFont
  7. import requests
  8. from piket_client.sound import PLOP_WAVE
  9. def plop() -> None:
  10. """ Asynchronously play the plop sound. """
  11. PLOP_WAVE.play()
  12. from piket_server import PEOPLE
  13. FONT = QFont("Helvetica", 15)
  14. def get_names() -> {int: (str, int)}:
  15. return PEOPLE
  16. class NameButton(QPushButton):
  17. """ Wraps a QPushButton to provide a counter. """
  18. def __init__(self, name: str, count: int, *args, **kwargs) -> None:
  19. self.name = name
  20. self.count = count
  21. super().__init__(self.current_label, *args, **kwargs)
  22. self.clicked.connect(self.plop)
  23. @property
  24. def current_label(self) -> str:
  25. return f"{self.name} ({self.count})"
  26. def plop(self) -> None:
  27. self.count += 1
  28. plop()
  29. self.setText(self.current_label)
  30. class NameButtons(QWidget):
  31. def __init__(self) -> None:
  32. super().__init__()
  33. self.layout = None
  34. self.init_ui()
  35. def init_ui(self) -> None:
  36. self.layout = QGridLayout()
  37. for index, id in enumerate(PEOPLE):
  38. person = PEOPLE[id]
  39. button = NameButton(person["name"], person["count"])
  40. self.layout.addWidget(button, index // 2, index % 2)
  41. self.setLayout(self.layout)
  42. if __name__ == "__main__":
  43. app = QApplication(sys.argv)
  44. font = app.font()
  45. size = font.pointSize()
  46. font.setPointSize(size * 1.75)
  47. app.setFont(font)
  48. name_buttons = NameButtons()
  49. name_buttons.show()
  50. app.exec_()