import sys import random import math 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) ] class RandomWheel(QWidget): def __init__(self): super().__init__() self.initUI() self.angle = 0 def initUI(self): self.setWindowTitle('Random Wheel') self.choices = ["Option 1", "Option 2", "Option 3", "Option 4", "Option 5", "Option 6"] 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: self.angle = 0 self.spin = False self.timer.stop() self.chosen_option = random.choice(self.choices) self.label.size 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_())