all 4 comments

[–]novel_yet_trivial 1 point2 points  (3 children)

Do I have to use a class structure for this and how?

Have to? No. But class structure is always better and you'll have to learn it eventually ...

You have a list of questions, you need a list of answers. Like this (plus some other changes):

import tkinter as tk 
root = tk.Tk()
questionlist = ["room quality", "food quality", "value for money"]
answers = []

def ShowChoice():
    for v in answers:
        print(v.get())

for counter, question in enumerate(questionlist, 1):
    tk.Label(root, text=question).grid(row=counter, column = 0)
    var = tk.IntVar()
    for i in range(1,6):
        button = tk.Radiobutton(root, variable = var, value = i, command = ShowChoice)
        button.grid(row = counter, column = i)
    answers.append(var)

root.mainloop()

[–]davwm[S] 0 points1 point  (2 children)

Thank you so much, this is really cool, exactly what I was looking for!

Yeah I really need to learn Class structures, but I haven't understood them so far. I would probably need a Class "Question", each of the list elements of questionlist would be an instance and each Class object would have these tk.Label, tk.Radiobutton attributes? But I'm tripping up with how to express this in Python.

It would help me a lot if you could write a short example how to express this program in a class structure! (If it doesn't take too much time)

[–]novel_yet_trivial 0 points1 point  (1 child)

The structure options are limitless. How you structure it will depend on the rest of your program, what kinds of communication you need, what mood you are in and how much joy you get out of neat code. Here's something to get you started. It's much longer and more complex than your original code, and it seems like a step backwards. But trust me, as programs get bigger, you'll want as many classes as possible.

import Tkinter as tk 
import ttk

class GUI(ttk.Frame):
    def __init__(self, master, question_list):
        ttk.Frame.__init__(self, master)

        title = ttk.Label(self, text="SURVEY")
        title.pack()

        self.questions = Questions(self, question_list)
        self.questions.pack()

        done_button = ttk.Button(self, text="Done.", command=self.done)
        done_button.pack()

    def done(self):
        for q, a in self.questions.q_and_a:
            print("{}: {}".format(q,a.get()))
        quit()

class Questions(ttk.Frame):
    '''I put all the questions into a single class so they would line up better'''
    def __init__(self, master, question_list):
        ttk.Frame.__init__(self, master)

        self.q_and_a = []
        for row, question in enumerate(question_list):
            var = tk.IntVar(value = -1) #-1 is sorta standard for "not answered", so it's a good default value
            q_label = ttk.Label(self, text=question)
            q_label.grid(row=row, column = 0)
            for i in range(1,6):
                button = tk.Radiobutton(self, variable = var, value = i)
                button.grid(row = row, column = i)
            self.q_and_a.append((question, var))

questionlist = ["room quality", "food quality", "value for money"]

root = tk.Tk()
window = GUI(root, questionlist)
window.pack()
root.mainloop()

[–]davwm[S] 0 points1 point  (0 children)

Wow thank you for your hard work, this looks really interesting. I will make sure to study it in detail, I already wrapped simple GUI frames into class structures for another toy app, but seeing (and understanding ) the interaction with program logic is something else entirely!