pythonuser-interfacepython-2.7tkintergui-designer

How to design GUI sequence in Tkinter with Python 2.7


I want to write a program that asks the user a series of questions in different dialog boxes. Each box shows up one at a time, and goes to the next box if the button next is clicked. My question is do I create a class for each Dialog and just call the next class once the button next is clicked? Or is there a more elegant solution to this?


Solution

  • My recommendation is to build a base class that holds the current question, then when the user answers a question and advances to the next one, the base class updates the display of the new current question. You don't need to destroy widgets at any point (except when quitting the application), and you can also reuse widgets before creating new ones.

    Let me simplify the format of your questions to this one: each question contains a question description and a set of answers where the user can pick a single one. These simplifications can be removed, but I set them in order to present an initial code to deal with the problem. Here is a starting point for doing exactly this (my imagination towards naming was weak):

    import random
    import Tkinter
    import ttk
    
    # Make 5 "questions" with varied number of answers to pick from.
    QUESTION = [u"Question %d" % (i + 1) for i in range(5)]
    QOPTS = []
    k = 1
    for _ in QUESTION:
        num_opts = random.randint(3, 6)
        QOPTS.append([u"text %d" % (k + i) for i in range(num_opts)])
        k += num_opts
    
    
    class Question:
        def __init__(self, master):
            self._common_var = Tkinter.StringVar()
    
            self._title = None
            self._lframe = None
            self._rb = []
            self._make_gui(master)
    
        def get_answer(self):
            return self._common_var.get()
    
        def reset_answer(self):
            self._common_var.set("")
    
        def _make_gui(self, master):
            self._title = ttk.Label(master, padding=(0, 6, 0, 0))
            self._title.grid(in_=master, padx=6, row=0, column=0, sticky='ew')
    
            self._lframe = ttk.Labelframe(master)
            self._lframe.grid(in_=master, padx=6, row=1, column=0, sticky='nsew')
    
        def update_gui(self, question, options):
            self._title['text'] = question
            for i, opt in enumerate(options):
                if i < len(self._rb):
                    if not self._rb[i].grid_info():
                        self._rb[i].grid()
                    self._rb[i]['text'] = opt
                else:
                    rb = ttk.Radiobutton(self._lframe, text=opt, value=i,
                            variable=self._common_var)
                    rb.grid(in_=self._lframe, row=i, column=0, sticky='w')
                    self._rb.append(rb)
            # Deal with i < total.
            for k in xrange(i + 1, len(self._rb)):
                self._rb[k].grid_remove()
    
    
    
    class Base:
        def __init__(self, frame, q, o):
            self.master = frame
    
            self.question = None
            self.curr_question = 0
            self.q = q
            self.o = o
            self._make_gui(frame)
            self._update_gui()
    
        def next_question(self):
            answer = self.question.get_answer()
            try:
                answer = int(answer)
            except ValueError:
                print "Question not answered, stay here."
                return
            print "Answer for question %d: %d" % (self.curr_question, answer)
            self.question.reset_answer()
    
            self.curr_question += 1
            self._update_gui()
    
        def _make_gui(self, frame):
            self.question = Question(frame)
            frame.columnconfigure(0, weight=1)
            frame.rowconfigure(1, weight=1)
    
            btn = [(u"Next", self.next_question)]
            self._btn = []
            for i, (text, cmd) in enumerate(btn):
                # Assumption: the Question instance always uses the rows 0 and 1.
                b = ttk.Button(frame, text=text, command=cmd)
                b.grid(in_=frame, padx=6, pady=6, row=2, column=i, sticky='e')
                self._btn.append(b)
    
        def _update_gui(self):
            if self.curr_question == len(self.q):
                print "Done"
                self.master.quit()
                return
            elif self.curr_question == len(self.q) - 1:
                for btn in self._btn:
                    # No next question
                    btn['text'] = u"Finish"
    
            self.question.update_gui(self.q[self.curr_question],
                    self.o[self.curr_question])
    
    
    root = Tkinter.Tk()
    root.columnconfigure(0, weight=1)
    root.rowconfigure(0, weight=1)
    root.geometry('300x250')
    frame = ttk.Frame(root)
    frame.grid(sticky='nsew')
    Base(frame, QUESTION, QOPTS)
    root.mainloop()
    

    And here is the GUI you will get:

    enter image description here enter image description here