add base class for grid, update main and participant
This commit is contained in:
parent
38a176cdbc
commit
3effce358c
22
main.py
22
main.py
|
|
@ -1,12 +1,30 @@
|
|||
import sys
|
||||
|
||||
from PyQt5.QtWidgets import QApplication
|
||||
from PyQt5.QtWidgets import QApplication, QStackedWidget
|
||||
|
||||
|
||||
from modules.participant import Participant
|
||||
from modules.grid import GridDots
|
||||
|
||||
|
||||
class MainWindow(QStackedWidget):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.participant_widget = Participant()
|
||||
self.grid_widget = GridDots()
|
||||
self.addWidget(self.participant_widget)
|
||||
self.addWidget(self.grid_widget)
|
||||
|
||||
self.participant_widget.reg_success.connect(self.show_line_widget)
|
||||
|
||||
def show_line_widget(self):
|
||||
"""Method to show widget with index 1 in the stack"""
|
||||
self.setCurrentIndex(1) # change to lines not grid later
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app = QApplication(sys.argv)
|
||||
window = Participant()
|
||||
window = MainWindow()
|
||||
window.setWindowTitle("3Dots")
|
||||
window.show()
|
||||
sys.exit(app.exec_())
|
||||
|
|
|
|||
58
modules/grid.py
Normal file
58
modules/grid.py
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
from PyQt5.QtGui import QPen, QBrush
|
||||
from PyQt5.QtCore import Qt, QPointF
|
||||
from PyQt5.QtWidgets import (
|
||||
QWidget,
|
||||
QGraphicsView,
|
||||
QGraphicsScene,
|
||||
QGraphicsEllipseItem,
|
||||
QGraphicsLineItem,
|
||||
QVBoxLayout
|
||||
)
|
||||
|
||||
|
||||
class GridDots(QWidget):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.view = QGraphicsView()
|
||||
self.scene = QGraphicsScene()
|
||||
self.scene.setSceneRect(0, 0, 800, 800)
|
||||
self.view.setScene(self.scene)
|
||||
|
||||
self.dots = []
|
||||
self.lines = set()
|
||||
self.setup_ui()
|
||||
self.create_grid()
|
||||
|
||||
def setup_ui(self):
|
||||
layout = QVBoxLayout(self)
|
||||
layout.addWidget(self.view)
|
||||
|
||||
def create_grid(self):
|
||||
"""Draw 3x3 grid of dots"""
|
||||
dot_radius = 30
|
||||
spacing = 240
|
||||
offset = 150
|
||||
|
||||
for row in range(3):
|
||||
for col in range(3):
|
||||
x, y, = offset + col * spacing, offset + row * spacing
|
||||
dot = QGraphicsEllipseItem(
|
||||
x - dot_radius, y - dot_radius,
|
||||
2 * dot_radius, 2 * dot_radius)
|
||||
dot.setBrush(QBrush(Qt.black))
|
||||
self.scene.addItem(dot)
|
||||
self.dots.append((x, y))
|
||||
|
||||
def draw_line(self, start, end):
|
||||
line_id = tuple(sorted((start, end)))
|
||||
if line_id not in self.lines:
|
||||
start_pos = QPointF(*self.dots[start])
|
||||
end_pos = QPointF(*self.dots[end])
|
||||
|
||||
line = QGraphicsLineItem(
|
||||
start_pos.x(), start_pos.y(),
|
||||
end_pos.x(), end_pos.y())
|
||||
|
||||
line.setPen(QPen(Qt.black, 2))
|
||||
self.scene.addItem(line)
|
||||
self.lines.add(line_id)
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
from PyQt5.QtCore import Qt, pyqtSignal
|
||||
from PyQt5.QtWidgets import (
|
||||
QWidget,
|
||||
QVBoxLayout,
|
||||
|
|
@ -13,10 +14,10 @@ from PyQt5.QtWidgets import (
|
|||
QSpinBox
|
||||
)
|
||||
|
||||
from PyQt5.QtCore import Qt
|
||||
|
||||
|
||||
class Participant(QWidget):
|
||||
reg_success = pyqtSignal()
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
|
|
@ -79,5 +80,11 @@ class Participant(QWidget):
|
|||
QMessageBox.warning(
|
||||
self, 'Input Error', 'Please select your sex.')
|
||||
else:
|
||||
QMessageBox.information(
|
||||
self, 'Success', f'Participant Registered: {identifier}, Age: {age}, Sex: {sex}')
|
||||
result = QMessageBox.information(
|
||||
self,
|
||||
'Success',
|
||||
f'Participant Registered: {identifier}, Age: {age}, Sex: {sex}'
|
||||
)
|
||||
|
||||
if result == QMessageBox.Ok:
|
||||
self.reg_success.emit()
|
||||
|
|
|
|||
Loading…
Reference in a new issue