Python Forum
[PyQt] Create a GUI for alrady written code.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] Create a GUI for alrady written code.
#1
I'm a new Python user and I'm still learning so I decided to write a simple quiz program in Pycharm.
Only problem is I can't find a tutorial anywhere showing me how to design a GUI in Qt designer without re-doing all of the quiz program's code.

passed = 0

correct_questions = 0

tries = 3

difficulty_selected = 0

while difficulty_selected == 0:
    difficulty = input(
        "Enter (E) for 3 attempts on each question, enter (N) for a total of 3 attempts or enter (H) for 1 attempt on each question: ")
    if difficulty == "E":
        print("Easy selected")
        difficulty_selected = "1"
    if difficulty == "N":
        print("Normal selected")
        difficulty_selected = "1"
    if difficulty == "H":
        print("Hard selected")
        difficulty_selected = "1"
        tries = 1
        print(tries, "Attempt remaining")
    if difficulty_selected == 0:
        print("Invalid selection")

# Question_1

a = "10"

b = "5 x 2 = "

question_1 = input(b)

while question_1 != a:
    tries -= 1
    if tries != 0:
        print("Wrong answer, try again")
    if tries > 1:
        print(tries, "Attempts remaining")
    if tries == 1:
        print(tries, "Attempt remaining")
    if tries == 0:
        break
    question_1 = input(b)
    if question_1 == a:
        break

if question_1 == a:
    print("Correct answer")
    passed = 1
    correct_questions += 1
else:
    if difficulty != "E":
        if tries > 0:
            print("Failed  question")
        if tries == 0:
            print("Failed")
    passed = 0
    if difficulty == "E":
        passed = 1
        print("Wrong answer, failed question")
    print("Correct answer = " + a)

# Question_2

a = "15"

b = "5 x 3 = "

if difficulty == "E":
    if tries < 3:
        print("Number of attempts reset")
        print("3 Attempts remaining")
        tries = 3

if difficulty == "E":
    if passed == 1:
        question_1 = input(b)

if difficulty == "N":
    if passed == 1:
        question_1 = input(b)

if difficulty == "H":
    if passed == 1:
        question_1 = input(b)

while question_1 != a:
    if passed == 0:
        break
    tries -= 1
    if tries != 0:
        print("Wrong answer, try again")
    if tries > 1:
        print(tries, "Attempts remaining")
    if tries == 1:
        print(tries, "Attempt remaining")
    if tries == 0:
        break
    question_1 = input(b)
    if question_1 == a:
        break

if question_1 == a:
    print("Correct answer")
    passed = 1
    correct_questions += 1
else:
    if passed == 1:
        if difficulty != "E":
            if tries > 0:
                print("Failed  question")
            if tries == 0:
                print("Failed")
        passed = 0
        if difficulty == "E":
            passed = 1
            print("Wrong answer, failed question")
        print("Correct answer = " + a)

# Question_3

a = "20"

b = "5 x 4 = "

if difficulty == "E":
    if tries < 3:
        print("Number of attempts reset")
        print("3 Attempts remaining")
        tries = 3

if difficulty == "E":
    if passed == 1:
        question_1 = input(b)

if difficulty == "N":
    if passed == 1:
        question_1 = input(b)

if difficulty == "H":
    if passed == 1:
        question_1 = input(b)

while question_1 != a:
    if passed == 0:
        break
    tries -= 1
    if tries != 0:
        print("Wrong answer, try again")
    if tries > 1:
        print(tries, "Attempts remaining")
    if tries == 1:
        print(tries, "Attempt remaining")
    if tries == 0:
        break
    question_1 = input(b)
    if question_1 == a:
        break

if question_1 == a:
    print("Correct answer")
    passed = 1
    correct_questions += 1
else:
    if passed == 1:
        if difficulty != "E":
            if tries > 0:
                print("Failed  question")
            if tries == 0:
                print("Failed")
        passed = 0
        if difficulty == "E":
            passed = 1
            print("Wrong answer, failed question")
        print("Correct answer = " + a)

# Question_4

a = "25"

b = "5 x 5 = "

if difficulty == "E":
    if tries < 3:
        print("Number of attempts reset")
        print("3 Attempts remaining")
        tries = 3

if difficulty == "E":
    if passed == 1:
        question_1 = input(b)

if difficulty == "N":
    if passed == 1:
        question_1 = input(b)

if difficulty == "H":
    if passed == 1:
        question_1 = input(b)

while question_1 != a:
    if passed == 0:
        break
    tries -= 1
    if tries != 0:
        print("Wrong answer, try again")
    if tries > 1:
        print(tries, "Attempts remaining")
    if tries == 1:
        print(tries, "Attempt remaining")
    if tries == 0:
        break
    question_1 = input(b)
    if question_1 == a:
        break

if question_1 == a:
    print("Correct answer")
    passed = 1
    correct_questions += 1
else:
    if passed == 1:
        if difficulty != "E":
            if tries > 0:
                print("Failed  question")
            if tries == 0:
                print("Failed")
        passed = 0
        if difficulty == "E":
            passed = 1
            print("Wrong answer, failed question")
        print("Correct answer = " + a)

if correct_questions > 1:
    print(correct_questions, "Questions correct")

if correct_questions == 1:
    print(correct_questions, "Question correct")

if tries > 0:
    print("Finished")
Reply
#2
The quiz code is written without consideration of being able to display it and interact with it with GUI code.
It would have to be modified to work with a GUI.
Reply
#3
(Jun-03-2020, 06:26 PM)Yoriz Wrote: The quiz code is written without consideration of being able to display it and interact with it with GUI code.
It would have to be modified to work with a GUI.

How would I then go about doing that?

I would like to have a GUI for the programs that I write but I can't find a tutorial that shows you how to write the code and also use Qt Designer for creating a GUI.
There's plenty of tutorials that show how to work with Qt Designer but none of them write an actual program.
Reply
#4
You would create a GUI frame for deciding on the difficulty.
It would have a label displaying information about the difficulty.
A Combobox with the available choices.
A button to submit the choice selection.
An event handler would be bound to the button, to read the selection from the Combobox.

Another frame would be created for displaying the questions.
A label would display a question.
An entry widget would be used for entering the result.
A submit button for once the answer is filled in.
A question would be displayed in the label
An event handler would be bound to the button, the value entered would be compared against the answer.
A popup dialog could display if the answer was correct or not.

The rest of the questions would be displayed one after another.

eventually, a score frame would display the results in labels.
Reply
#5
Is there documentation that can show me how to do that with Qt Designer?
Reply
#6
Forget about Qt Designer and Python. The first step it designing a game is to storyboard the interface. Think about what you want to do before you think about how you are going to do it. If you are thinking about implementation when you should be thinking about design you end up with a crappy design.

After you have your storyboard you have your interface. Here you can start up QtDesigner and see how well you can make a panel(s) that look like what you want. I still wouldn't worry too much about writing the underlying code.

Once your panels all look the way you want, then you can start reading about how you can tie controls in a QtDesigner panel to underlying code.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create an identification code from user input PeroPuri 1 1,898 Apr-11-2020, 11:56 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020