Python Öğreniyorum 37 – PyQt5 ile Quiz hazırlama ChatGPT
Python Öğreniyorum 37 – PyQt5 ile Quiz hazırlama ChatGPT. Bu videomuzda ChatGPT kullanarak Python programlama dilinde PyQt5 kütüphanesi ile bir quiz oluşturdur. Sorular ve cevap şıkları ekrana rastgele olarak tek tek geliyor. Test sonunda kaç doğru olduğunu gösteriyor. Yeniden başlama butonu imkanı veriyor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
import sys import random from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QLabel, QRadioButton, QPushButton, QButtonGroup, QMessageBox from PyQt5.QtCore import Qt, QCoreApplication, QRect class QuizWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle('Quiz Uygulaması') self.setGeometry(QRect(0, 0, 400, 300)) # Pencere boyutunu sabitler self.questions = [ { 'question': 'Hangi gezegen Güneş Sistemi\'nde üçüncü sıradadır?', 'choices': ['Mars', 'Venüs', 'Dünya', 'Jüpiter'], 'correct_answer': 'Dünya' }, { 'question': 'Hangi hayvan uçabilir?', 'choices': ['At', 'Köpek', 'Kuş', 'Yılan'], 'correct_answer': 'Kuş' }, { 'question': 'Hangi renk üç renkli trafik lambasında ortada yer alır?', 'choices': ['Kırmızı', 'Sarı', 'Yeşil', 'Mavi'], 'correct_answer': 'Sarı' } ] self.current_question = 0 self.score = 0 random.shuffle(self.questions) # Soruları rastgele sıralama self.question_label = QLabel() self.question_label.setAlignment(Qt.AlignCenter) self.question_number_label = QLabel() self.question_number_label.setAlignment(Qt.AlignCenter) self.radio_buttons = [] self.button_group = QButtonGroup() self.button_group.setExclusive(False) layout = QVBoxLayout() layout.addWidget(self.question_number_label) layout.addWidget(self.question_label) for choice in self.questions[self.current_question]['choices']: radio_button = QRadioButton(choice) self.radio_buttons.append(radio_button) self.button_group.addButton(radio_button) layout.addWidget(radio_button) self.next_button = QPushButton('Sonraki Soru') self.next_button.clicked.connect(self.next_question) self.restart_button = QPushButton('Yeniden Başlat') self.restart_button.clicked.connect(self.restart_quiz) layout.addWidget(self.next_button) layout.addWidget(self.restart_button) widget = QWidget() widget.setLayout(layout) self.setCentralWidget(widget) self.update_question() def update_question(self): self.question_number_label.setText(f"Soru {self.current_question + 1}") self.question_label.setText(self.questions[self.current_question]['question']) choices = self.questions[self.current_question]['choices'] random.shuffle(choices) # Cevap şıklarını rastgele sıralama for i, choice in enumerate(choices): self.radio_buttons[i].setText(choice) if self.current_question == len(self.questions) - 1: self.next_button.setText('Sonuç') def next_question(self): selected_answer = None for radio_button in self.radio_buttons: if radio_button.isChecked(): selected_answer = radio_button.text() radio_button.setChecked(False) break if selected_answer == self.questions[self.current_question]['correct_answer']: self.score += 1 self.current_question += 1 if self.current_question < len(self.questions): self.update_question() self.clear_radio_buttons() else: # Tüm soruları tamamladık self.show_result() def clear_radio_buttons(self): self.button_group.setExclusive(False) for radio_button in self.radio_buttons: radio_button.setChecked(False) self.button_group.setExclusive(True) def restart_quiz(self): random.shuffle(self.questions) # Soruları yeniden rastgele sıralama self.current_question = 0 self.score = 0 self.update_question() self.clear_radio_buttons() self.next_button.setEnabled(True) self.next_button.setText('Sonraki Soru') def show_result(self): result_message = f'Skorunuz: {self.score}/{len(self.questions)}' QMessageBox.information(self, 'Quiz Sonucu', result_message) app = QApplication(sys.argv) quiz_window = QuizWindow() # Pencereyi ekranın ortasına yerleştirme screen_geometry = app.desktop().availableGeometry() x = (screen_geometry.width() - quiz_window.width()) // 2 y = (screen_geometry.height() - quiz_window.height()) // 2 quiz_window.move(x, y) quiz_window.show() sys.exit(app.exec_()) |