Python Forum

Full Version: how exactly do i get my question and answer to work in my program
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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()
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.
(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 ?
If there were a trivial way you would have found it out all by yourself:)
(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
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