Python Forum

Full Version: Modify code from running program ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello i want to ask for help. I have simple quiz program with pyqt5. PRogram asks question and you fill a,b,c answers and it gives you your result in the end. I want to make extension window where i can add more questions to quiz. In my code i have list of questions and answers. I want to add more of them in running program.

Please can you guys help me with this?
you can start by showing existing code.
import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QMainWindow,QDialog,QBoxLayout, QApplication, QWidget, QPushButton, QAction, QLineEdit, QMessageBox, QComboBox,QProgressBar,QLabel,QPlainTextEdit
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.Qt import QApplication
from PyQt5.QtGui import QDesktopServices
from PyQt5.QtCore import QUrl
import subprocess
import time
import random
import threading
class Window(QMainWindow):

   def __init__(self):
       super().__init__()
       self.question_prompts = [
           "What color are apples ?\n(a) Red/Green\n(b) Yellow\n(c) Pink\n",
           "What color are bananas ?\n(a) Teal\n(b) Yellow\n(c) Black\n",
           "What color are strawberry ?\n(a) yelow\n(b) black\n(c) red\n",
           "What color are toilets ?\n(a) yelow\n(b) black\n(c) red\n",
           "What color is ferrari ?\n(a) yelow\n(b) black\n(c) red\n"
       ]
       self.numberof = len(self.question_prompts)
       self.answers = ["a","b","c","d","e"]
       self.answers1= -1
       self.score = 0
       self.counter = 0
       self.setFixedSize(400, 200)
       self.move(100,100)
       self.setWindowTitle("quiz")


       self.b1 = QtWidgets.QPushButton(self)
       self.b1.setText("Next")
       self.b1.move(240, 120)
       self.b1.resize(90, 32)
       self.b1.clicked.connect(self.next)

       self.b2 = QtWidgets.QPushButton(self)
       self.b2.setText("Back")
       self.b2.move(150, 120)
       self.b2.resize(90, 32)
       self.b2.clicked.connect(self.back)
       self.b2.setDisabled(False)

       self.textbox1 = QLineEdit(self)
       self.textbox1.move(50, 110)
       self.textbox1.resize(100, 32)
       self.textbox1.setPlaceholderText("Answer")
       self.textbox1Value = self.textbox1.text()


       self.label2 = QtWidgets.QLabel(self)
       self.label2.move(48, 40)
       self.label2.resize(130, 70)
       self.label2.setText(str(self.question_prompts[self.counter]))
       self.update()

       self.show()

   def next(self):
       if self.counter < self.numberof-1:
           self.counter += 1
           self.answers1 +=1
           self.label2.setText(str(self.question_prompts[self.counter]))
           if self.textbox1.text() == self.answers[self.answers1]:
               self.score += 1
           self.textbox1.clear()

       else:
           if self.textbox1.text() == self.answers[self.counter]:
               self.score += 1
           self.label2.setText("You got " + str(self.score) + " from " + str(len(self.question_prompts)) + " right ")
           self.counter +=1
           self.b1.disconnect()


   def back(self):
       if self.counter != self.numberof and self.counter !=0:
           self.counter = self.counter - 1
           self.answers1 = self.answers1 -1
           self.label2.setText(str(self.question_prompts[self.counter]))


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Window()
    sys.exit(app.exec())