Python Forum
how exactly do i get my question and answer to work in my program
Thread Rating:
  • 2 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how exactly do i get my question and answer to work in my program
#1
ok so i need to create a program in tkinter with upto 50 question and 4 options respective to each question now for testing i have added loops but now i need to add the actual quiz questions and options for the same and i am stuck here as to how exactly do i get it working properly i need to shuffle the options provided (questions as well if its possible)

Ps- i am new to tkinter many things might seems complex to me but i am trying to understand

try:
    import tkinter as tk
    from tkinter import messagebox
except ImportError:
    import Tkinter as tk
    import tkMessageBox as messagebox
 
class VerticalScrolledFrame:
 
    def __init__(self, master, **kwargs):
        self.outer = tk.Frame(master)
 
        self.vsb = tk.Scrollbar(self.outer, orient=tk.VERTICAL)
        self.vsb.pack(fill=tk.Y, side=tk.RIGHT)
        self.canvas = tk.Canvas(self.outer, highlightthickness=0, **kwargs)
        self.canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
        self.canvas['yscrollcommand'] = self.vsb.set
        self.canvas.bind("<Enter>", self._bind_mouse)
        self.canvas.bind("<Leave>", self._unbind_mouse)
        self.vsb['command'] = self.canvas.yview
 
        self.inner = tk.Frame(self.canvas)
        self.canvas.create_window(4, 4, window=self.inner, anchor='nw')
        self.inner.bind("<Configure>", self._on_frame_configure)
 
        self.outer_attr = set(dir(tk.Widget))
 
    def __getattr__(self, item):
        if item in self.outer_attr:
            return getattr(self.outer, item)
        else:
            return getattr(self.inner, item)
 
    def _on_frame_configure(self, event=None):
        self.canvas.configure(scrollregion=self.canvas.bbox("all"))
 
    def _bind_mouse(self, event=None):
        self.canvas.bind_all("<4>", self._on_mousewheel)
        self.canvas.bind_all("<5>", self._on_mousewheel)
        self.canvas.bind_all("<MouseWheel>", self._on_mousewheel)
 
    def _unbind_mouse(self, event=None):
        self.canvas.unbind_all("<4>")
        self.canvas.unbind_all("<5>")
        self.canvas.unbind_all("<MouseWheel>")
 
    def _on_mousewheel(self, event):
        """Linux uses event.num; Windows / Mac uses event.delta"""
        if event.num == 4 or event.delta == 120:
            self.canvas.yview_scroll(-1, "units" )
        elif event.num == 5 or event.delta == -120:
            self.canvas.yview_scroll(1, "units" )
 
root=tk.Tk()
root.geometry('700x700+400+400')
root.title('Quiz Program')
 
welcom=tk.Label(root,text="Welcome To Quiz")
welcom.pack()
 
def mquit():
    mexit=messagebox.askyesno(title="Quit",message="Quit The Test ?")
    if mexit > 0:
        root.destroy()
        return
 
answers = {}
labels = {}
question_frame = VerticalScrolledFrame(root, height=500)
question_frame.pack(fill=tk.X)
 
for i in range(1, 26):
    answers[i] = tk.IntVar()
    labels[i] = tk.Label(question_frame, text="This Is Question %d :" % i)
    labels[i].pack()
    for j in range(1, 5):
        btn = tk.Radiobutton(question_frame,
            text="Option %d" % j,
            value=j,
            variable=answers[i])
        btn.pack()
 
#i need to add four options and point out 1 right answer out of 4 option provided
#labels will also change as i'll add each question into the program (upto 50questions)
 
mbutton=tk.Button(root,text='Quit',command=mquit,fg='red',bg='blue')
mbutton.pack()
 
root.mainloop()
Reply
#2
1) You need to keep a list of all the Radiobutton (possibly a list of lists) that you create
2) You can derive the Radiobutton class to add a "correct answer" attribute and use instance of that class Radiobutton in your app (you set that attribute on the button which is the correct answer for the question)
3) You collect the results by scanning the list of button, and checking the state of the Radiobutton against the "correct answer" attribute.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#3
(Apr-12-2017, 07:48 AM)Ofnuts Wrote: 1) You need to keep a list of all the Radiobutton (possibly a list of lists) that you create
2) You can derive the Radiobutton class to add a "correct answer" attribute and use instance of that class Radiobutton in your app (you set that attribute on the button which is the correct answer for the question)
3) You collect the results by scanning the list of button, and checking the state of the Radiobutton against the "correct answer" attribute.

sounds bit complex any easy way ?
Reply
#4
If there were a trivial way you would have found it out all by yourself:)
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#5
(Apr-12-2017, 03:18 PM)Ofnuts Wrote: If there were a trivial way you would have found it out all by yourself:)

atleast some example or somthing ? so i get the idea as to how do i create a function accordingly
Reply
#6
check out how the various widgets work with this tutorial

example code for each


[Image: zip.png]   TkinterDemo.zip (Size: 179.65 KB / Downloads: 13)

Start from command line with:
python index.py
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Answer for newbie, Sqlite3 Froger 2 812 Sep-27-2023, 05:33 PM
Last Post: noisefloor
  Coding answer for newbie cg3 2 1,112 Aug-05-2023, 12:17 PM
Last Post: jefsummers
  What am I doing wrong to not get the answer pav1983 7 3,635 Jun-25-2020, 08:53 PM
Last Post: ndc85430
  Need help with an assignment, not an answer. celtickodiak 4 2,899 Oct-01-2019, 02:04 PM
Last Post: jefsummers
  A program for backing up documents (doesnt work(code is inside)) Richard_SS 4 3,366 Jun-05-2019, 03:47 PM
Last Post: heiner55
  What's the full answer to this question in python? (bayesian decision theory) Hblaugrana 1 2,435 Oct-31-2018, 02:22 PM
Last Post: j.crater
  Functions and program question Zei 7 4,867 Aug-31-2017, 01:39 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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