Python 3 and Tkinter- how to control a quiz loop

Python 3 and Tkinter- how to control a quiz loop

By : user2953965
Date : November 21 2020, 07:31 AM
will be helpful for those in need 1) You are printing the question before you increment i. That's why you get twice.
2) It is always 1 becuase of your usage of global. In python you use global keyword in which scope you want to change your variable. Sadly, my english is not good enough to explain this. Please check out these answers.
code :
from tkinter import *

questions =["What is the name of the Simpsons' next door neighbour?","What is the name of the    school bus driver?",
        "Who runs the Kwik-e-mart?","What does Bart do at the end of the opening credits?"]

answers = [ "Ned Flanders","Otto","Apu","Write On The Blackboard"]

#removed globals from here
score = 0
i = 0

def submit():
    '''runs the submit button'''
    global i
    global score

    if answer.get().lower()==answers[i].lower():

    i+=1 #first increment, then show the question since you already show it at startup

    try:  #since you get the IndexError on this line, I used on here
    except IndexError:
        print ("something")

    scoretxt.config(text = "Your score is: {}".format(str(score)))

window = Tk()
window.title("Simpsons Quiz")
window.configure(background ="#ffd600")

banner = PhotoImage(file ="the-simpsons-banner.gif")
Label(window,image = banner).grid(row = 0,columnspan = 6)
Label(window,text = "Question : ",bg ="#ffd600",justify=LEFT).grid(row = 1,column = 0)
Label(window,text = "Type answer here: ",bg = "#ffd600",justify=LEFT).grid(row = 3, column = 0)
scoreLabel =Label(window,bg = "#ffd600")
scoretxt = Label(window,text ="Your score is: ?",bg = "#ffd600")
scoreLabel.grid(row=5,column = 2)
scoretxt.grid(row = 6,column = 2)
question=Label(window,bg = "white",text= questions[0],justify=LEFT)
question.grid(row =1,column=1)
answer = Entry(window,bg ="white",width = 30)
answer.grid(row = 3,column=1)

# make a submit button

Button(window,text= "Submit",bg = "white",command = submit).grid(row = 3,column = 2)


Share : facebook icon twitter icon
Python tkinter quiz

Python tkinter quiz

By : Maxime Florin
Date : March 29 2020, 07:55 AM
wish helps you Please note that, ideal way to go would be using classes.
You can define a function, inside of a function.
code :
import tkinter as tk
window = tk.Tk()
window.title("6 Questions")
score = 0
def inst():
    t = tk.Label(window, text="All you need to do is just answer each question with either a '1, 2, 3' or the actual word.")

def start():
    def submit():
        print (ans.get())
        #or do whatever you like with this

    root = tk.Tk()
    root.title("question 1")
    q = tk.Label(root, text="what type of input holds whole numbers")
    a = tk.Label(root, text="1.) int")
    b = tk.Label(root, text="2.) string")
    c = tk.Label(root, text="3.) float")
    ans = tk.Entry(root, width=40)
    #here is the button I want to verify the answer
    sub = tk.Button(root, text="Submit", command=submit)

greet = tk.Label(window, text="Welcome to the 6 Question Quiz.")
startButton = tk.Button(window, command=start, text="Start")
instr = tk.Button(window, text="Instructions", command=inst)
end = tk.Button(window, text="Exit", command=window.destroy)

Quiz in Tkinter, fixing if ... else loop

Quiz in Tkinter, fixing if ... else loop

By : Антон Тарануха
Date : March 29 2020, 07:55 AM
Hope this helps As Rawing commented, the code is comparing tkinter.Entry object to an object.
You can get the text entered to Entry using Entry.get(), you can convert the string to int object using int():
code :
def Validate():
    global score
    answer_string = answer.get()
        answer_value = int(answer_string)
    except ValueError as e:

    if answer_value == number1 * number2:
        score -= 1
        score += 1
Python Quiz using Tkinter

Python Quiz using Tkinter

By : Morten Kirstein
Date : March 29 2020, 07:55 AM
wish helps you flag is a tkinter IntVar, so flag == 3 will never be true.
Instead, use flag.get() == 3.
Adding to a counter on each frame for a quiz in python tkinter?

Adding to a counter on each frame for a quiz in python tkinter?

By : user1682721
Date : March 29 2020, 07:55 AM
should help you out You initialise MathsEnd when you initialise the app, so the label that shows the score uses the value of controller.correct at the start of the program, which is 0. You need to create a method in your MathsEnd class which updates the label:
code :
class MathsEnd(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        # create label, pack seperately so it can be configured later
        self.score = tk.Label(self)
        self.score.grid(column=1, row=1)
        # initalise using method

        tk.Button(self, text="Back To Home",
                        command = lambda: controller.show_frame("MainPage")) 
        .grid(column=6, row=10, sticky="nesw", pady=10)

    # new method to update label contents
    def update_label(self):
        self.score.config(text='Congrats you got %s/2' % self.controller.correct)
def right(self):
    self.controller.correct += 1
    # update score label

Python multiple choice quiz scoring using tkinter

Python multiple choice quiz scoring using tkinter

By : M.Shobana
Date : March 29 2020, 07:55 AM
around this issue I believe this is a situation where tkinter's Variable Classes would be of help, specifically, the IntVar class. I've reworked your code below to make total an IntVar but reduced it to three questions to simplify the example:
code :
from tkinter import *
from tkinter import ttk

def main():

    notebook.add(frame1, text="One")
    notebook.add(frame2, text="Two")
    notebook.add(frame3, text="Three")

    Label(frame1, text="What is Tkinter?").grid(row=2, column=2)
    Button(frame1, text="Guided User Interface", command=correct1).grid(row=3, column=1)
    Button(frame1, text="Variable", command=incorrect1).grid(row=3, column=2)
    Button(frame1, text="Function", command=incorrect1).grid(row=3, column=3)

    Label(frame2, text="What is Turtle?").grid(row=2, column=2)
    Button(frame2, text="Guided User Interface", command=incorrect2).grid(row=3, column=1)
    Button(frame2, text="Module", command=correct2).grid(row=3, column=2)
    Button(frame2, text="Boolean Value", command=incorrect2).grid(row=3, column=3)

    Label(frame3, text="What does the 'Print' command do?").grid(row=2, column=2)
    Button(frame3, text="Create a window", command=incorrect3).grid(row=3, column=1)
    Button(frame3, text="Show a message in the Python Shell", command=correct3).grid(row=3, column=2)
    Button(frame3, text="Print to the printer", command=incorrect3).grid(row=3, column=3)


    Label(root, text="Total:").pack()
    Label(root, textvariable=total).pack()

def correct1():
    Label(frame1, text="Correct").grid(row=1, column=2)

def incorrect1():
    Label(frame1, text="Incorrect").grid(row=1, column=2)

def correct2():
    Label(frame2, text="Correct").grid(row=1, column=2)

def incorrect2():
    Label(frame2, text="Incorrect").grid(row=1, column=2)

def correct3():
    Label(frame3, text="Correct").grid(row=1, column=2)

def incorrect3():
    Label(frame3, text="Incorrect").grid(row=1, column=2)

def counter():
    total.set(total.get() + 1)

root = Tk()

total = IntVar()  # defaults to 0

notebook = ttk.Notebook(root)

frame1 = ttk.Frame(notebook)
frame2 = ttk.Frame(notebook)
frame3 = ttk.Frame(notebook)


Related Posts Related Posts :
  • Remove commas in a string, surrounded by a comma and double quotes / Python
  • How to chain Django querysets preserving individual order
  • Comparison with Python
  • How to find backlinks in a website with python
  • Return new instance of subclass when using methods inherited from parent class in Python
  • Which function in django.contrib.auth creates the default model permissions?
  • Formatting text in tabular form with Python
  • How to determine the first day of a month in Python
  • Error while converting date to timestamp in python
  • Python string iterations
  • Is there any file number limitation when you select multiple files with wxFileDialog?
  • Errors with Matplotlib when making an executable with Py2exe (Python)
  • Django Haystack - Indexing single field
  • Go Pro Hero 3 - Streaming video over wifi
  • Appending a column in .csv with Python/Pandas
  • How to change my result directory in Robot framework using RIDE?
  • problem with using pandas to manipulate a big text file in python
  • python-magic module' object has no attribute 'open'
  • Where goes wrong for this High Pass Filter in Python?
  • Why inserting keys in order into a python dict is faster than doint it unordered
  • flann index saving in python
  • Create new instance of list or dictionary without class
  • How can I easily convert FORTRAN code to Python code (real code, not wrappers)
  • Address of lambda function in python
  • Python adding space between characters in string. Most efficient way
  • python http server, multiple simultaneous requests
  • Disguising username & password on distributed python scripts
  • Post GraphQL mutation with Python Requests
  • Why doesnt pandas create an excel file?
  • Rolling comparison between a value and a past window, with percentile/quantile
  • How to avoid repetitive code when defining a new type in python with signature verification
  • How to configure uWSGI in order to debug with pdb (--honour-stdin configuration issue)
  • In Python, how do you execute objects that are functions from a list?
  • Python- Variable Won't Subtract?
  • Processing Power In Python
  • Python 2.7.2 - Cannot import name _random or random from sys
  • Why doesn't the Python sorted function take keyword order instead of reverse?
  • Make a function redirect to other functions depending on a variable
  • get_absolute_url in django-categories
  • Monitoring non-Celery background task with New Relic in Python
  • Feature selection with LinearSVC
  • LSTM - Predicting the same constant values after a while
  • Test the length of elements in a list
  • Django: render radiobutton with 3 columns, cost column must change according to size & quantity selected
  • Python class attributes vs global variable
  • sys.stdout.writelines("hello") and sys.stdout.write("hello")
  • is ndarray faster than recarray access?
  • Python - search through directory trees, rename certain files
  • GAE: How to build a query where a string begins with a value
  • TypeError: __init__() takes at least 2 arguments (1 given)
  • Overriding and customizing "django.contrib.auth.views.login"
  • Django : Redirect to a particular page after login
  • Python search and copy files in directory
  • pretty printing numpy ndarrays using unicode characters
  • Frequent pattern mining in Python
  • How can I make a set of functions that can be used synchronously as well as asynchronously?
  • Convert one dice roll to two dice roll
  • count occourrence in a list
  • Writing an If condition to filter out the first word
  • to read file and compare column in python
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org