Finished basis of both the CLI and the GUI app
This commit is contained in:
parent
211fdb1077
commit
f0e75b9f0f
@ -19,3 +19,5 @@ Or you can create a json file with this structure:
|
||||
```
|
||||
And running:
|
||||
`python picker.py --file file.json`
|
||||
|
||||
TODO: Add argparse
|
||||
|
24
src/cli.py
Normal file
24
src/cli.py
Normal file
@ -0,0 +1,24 @@
|
||||
import random
|
||||
import time
|
||||
import json
|
||||
|
||||
def display_animation(choices):
|
||||
frames = ["-","\\","|","/"]
|
||||
for _ in range(20):
|
||||
for frame in frames:
|
||||
print(f"\rCalculating... {random.choice(choices)} {frame}", end="")
|
||||
time.sleep(0.1)
|
||||
|
||||
def generate_random_number():
|
||||
print("Generating a random number...")
|
||||
with open("demo.json", "r") as f:
|
||||
choices = json.loads(f.read())["choices"]
|
||||
|
||||
display_animation(choices)
|
||||
print(f"\rThe choice is: {random.choice(choices)}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
generate_random_number()
|
||||
except KeyboardInterrupt:
|
||||
pass
|
14
src/demo.json
Normal file
14
src/demo.json
Normal file
@ -0,0 +1,14 @@
|
||||
{
|
||||
"choices": [
|
||||
"Choice 5",
|
||||
"Choice 8",
|
||||
"Choice 7",
|
||||
"Choice 3",
|
||||
"Choice 9",
|
||||
"Choice 6",
|
||||
"Choice 10",
|
||||
"Choice 2",
|
||||
"Choice 1",
|
||||
"Choice 4"
|
||||
]
|
||||
}
|
115
src/gui.py
Normal file
115
src/gui.py
Normal file
@ -0,0 +1,115 @@
|
||||
import sys
|
||||
import random
|
||||
import math
|
||||
import json
|
||||
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLabel, QVBoxLayout
|
||||
from PyQt5.QtGui import QPainter, QColor, QPen, QFont
|
||||
from PyQt5.QtCore import Qt, QTimer
|
||||
|
||||
colors = [
|
||||
QColor(255, 0, 0), # Red: (255, 0, 0)
|
||||
QColor(255, 255, 0), # Yellow: (255, 255, 0)
|
||||
QColor(0, 0, 255), # Blue: (0, 0, 255)
|
||||
QColor(0, 255, 0) # Green: (0, 255, 0)
|
||||
]
|
||||
|
||||
SPIN_TIMES = 3
|
||||
|
||||
class RandomWheel(QWidget):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.initUI()
|
||||
self.angle = 0
|
||||
|
||||
def initUI(self):
|
||||
self.setWindowTitle('Random Wheel')
|
||||
with open("demo.json", "r") as f:
|
||||
self.choices = json.loads(f.read())["choices"]
|
||||
self.chosen_option = ""
|
||||
|
||||
self.button = QPushButton('Spin', self)
|
||||
self.button.clicked.connect(self.spin_wheel)
|
||||
|
||||
self.label = QLabel(self)
|
||||
self.label.setAlignment(Qt.AlignCenter)
|
||||
self.label.setFont(QFont('Arial', 14))
|
||||
|
||||
vbox = QVBoxLayout()
|
||||
vbox.addWidget(self.button)
|
||||
vbox.addWidget(self.label)
|
||||
self.setLayout(vbox)
|
||||
|
||||
self.timer = QTimer()
|
||||
self.timer.timeout.connect(self.update_wheel)
|
||||
self.angle = 0
|
||||
self.spin = False
|
||||
|
||||
def paintEvent(self, event):
|
||||
qp = QPainter()
|
||||
qp.begin(self)
|
||||
self.draw_wheel(qp)
|
||||
qp.end()
|
||||
|
||||
|
||||
def draw_wheel(self, qp):
|
||||
width = self.width()
|
||||
height = self.height()
|
||||
|
||||
wheel_radius = min(width, height) // 2 - 50
|
||||
center_x = width // 2
|
||||
center_y = height // 2
|
||||
num_choices = len(self.choices)
|
||||
angle_per_choice = 360 / num_choices
|
||||
|
||||
pen = QPen(Qt.black, 2)
|
||||
qp.setPen(pen)
|
||||
qp.setRenderHint(QPainter.Antialiasing)
|
||||
|
||||
for i in range(num_choices):
|
||||
start_angle = i * angle_per_choice + self.angle
|
||||
end_angle = (i + 1) * angle_per_choice + self.angle
|
||||
|
||||
color = colors[i % len(colors)]
|
||||
qp.setBrush(color)
|
||||
|
||||
qp.drawPie(center_x - wheel_radius, center_y - wheel_radius, wheel_radius * 2, wheel_radius * 2,
|
||||
int(start_angle * 16), int(angle_per_choice * 16))
|
||||
|
||||
# Calculate the position to display the option text
|
||||
text_angle = math.radians(start_angle + angle_per_choice / 2)
|
||||
text_radius = wheel_radius * 0.5
|
||||
text_x = center_x + int(text_radius * math.cos(text_angle))
|
||||
text_y = center_y - int(text_radius * math.sin(text_angle))
|
||||
|
||||
qp.setPen(Qt.black)
|
||||
qp.setFont(QFont('Arial', 12, QFont.Bold))
|
||||
qp.drawText(text_x - 50, text_y - 10, 100, 20, Qt.AlignCenter, self.choices[i])
|
||||
|
||||
def spin_wheel(self):
|
||||
if not self.spin:
|
||||
self.spin = True
|
||||
self.timer.start(30)
|
||||
self.angle_speed = 10 # Adjust the wheel spin speed as desired
|
||||
|
||||
def update_wheel(self):
|
||||
self.angle += self.angle_speed
|
||||
|
||||
if self.spin:
|
||||
if self.angle >= 360 * SPIN_TIMES:
|
||||
self.angle = 0
|
||||
self.spin = False
|
||||
self.timer.stop()
|
||||
self.chosen_option = random.choice(self.choices)
|
||||
self.label.setText(self.chosen_option)
|
||||
self.label.setAlignment(Qt.AlignBottom)
|
||||
self.angle_speed = 0 # Stop spinning the wheel
|
||||
|
||||
self.update()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app = QApplication(sys.argv)
|
||||
wheel = RandomWheel()
|
||||
wheel.setGeometry(400, 200, 400, 400)
|
||||
wheel.show()
|
||||
sys.exit(app.exec_())
|
Loading…
x
Reference in New Issue
Block a user