Python StringVar - get函数什么都没有返回?

时间:2013-11-18 16:39:37

标签: python python-3.x tkinter

我正在使用Python中的tkinter模块创建一个GUI应用程序(主要是为了提高我的Python技能,我们在GCSE计算机科学中使用它),但是我遇到了一个令人烦恼的问题,我无法弄清楚。

我有一个UI,它包含多个标签和使用网格方法放置的输入框(可能无关紧要,但仍然如此)。还有4个单选按钮。单击UI上的按钮(在本例中为“下一步”按钮)时,相关输入框中的值将存储在字典中,然后将其附加到类中的列表中。

当我将值存储到字典中时,我的问题出现了 - 我从附加到radiobuttons的StringVar()得到的值总是空的 - 一个空字符串,它会出现。

有问题的课程如下。我正在调用get()函数中的next(self)方法。

为什么会发生这种情况,我该如何解决?

CreateQuizApp.py:

from tkinter import *
from tkinter import messagebox
from tkinter import filedialog
from TriviaTools import TriviaTools

class CreateQuizApp(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)

        self.parent = parent
        self.parent.deiconify()

        self.parent.geometry("600x300")
        self.parent.title("New Quiz")

        self.title=""
        self.questions=[]

        self.initUI()

    def initUI(self):        
        if self.title is "": 
            titleLabel = Label(self.parent, text="Enter Quiz Title:")
            titleLabel.grid(row=0, column=0, sticky=W+E)

            titleEntry = Entry(self.parent)
            titleEntry.grid(row=0, column=1, sticky=W+E)

            nextButton = Button(self.parent, text="Next", command=self.next)
            nextButton.grid(columnspan=2, sticky=W+E)

            self.questionLabel = Label(self.parent, text="Enter Question:")

            self.questionEntry = Entry(self.parent)

            self.parent.grid_rowconfigure(0, weight=1)
            self.parent.grid_columnconfigure(0, weight=1)
            self.parent.grid_columnconfigure(1, weight=1)

            self.answer1Label = Label(self.parent, text="Enter Answer 1:")
            self.answer2Label = Label(self.parent, text="Enter Answer 2:")
            self.answer3Label = Label(self.parent, text="Enter Answer 3:")
            self.answer4Label = Label(self.parent, text="Enter Answer 4:")

            self.answer1Entry = Entry(self.parent)
            self.answer2Entry = Entry(self.parent)
            self.answer3Entry = Entry(self.parent)
            self.answer4Entry = Entry(self.parent)

            self.categoryLabel = Label(self.parent, text="Category:")
            self.categoryEntry = Entry(self.parent)

            self.correctAnswer = StringVar()

            self.correctAnswerLabel = Label(self.parent, text="Correct Answer:")
            self.correctAnswer1 = Radiobutton(self.parent, text="1", variable=self.correctAnswer, value="1", anchor=CENTER)
            self.correctAnswer2 = Radiobutton(self.parent, text="2", variable=self.correctAnswer, value="2", anchor=CENTER)
            self.correctAnswer3 = Radiobutton(self.parent, text="3", variable=self.correctAnswer, value="3", anchor=CENTER)
            self.correctAnswer4 = Radiobutton(self.parent, text="4", variable=self.correctAnswer, value="4", anchor=CENTER)

            self.correctAnswer1.select()
            self.correctAnswer2.deselect()
            self.correctAnswer3.deselect()
            self.correctAnswer4.deselect()

            self.pointsLabel = Label(self.parent, text="Points:")
            self.penaltyLabel = Label(self.parent, text="Penalty:")

            self.pointsEntry = Entry(self.parent)
            self.penaltyEntry = Entry(self.parent)

            self.explanationLabel = Label(self.parent, text="Explanation:")

            self.explanationEntry = Entry(self.parent)

            #self.correctAnswer.set(1)

            self.titleLabel = titleLabel
            self.titleEntry = titleEntry
            self.nextButton = nextButton
        else:
            try:
                self.titleEntry.destroy()
                self.titleLabel.destroy()
            except:
                pass

            self.nextButton.grid_forget()

            self.categoryLabel.grid(row=0, column=0, sticky=W+E)
            self.categoryEntry.grid(row=0, column=1, columnspan=4, sticky=W+E)

            self.questionLabel.grid(row=2, column=0, sticky=W+E)
            self.questionEntry.grid(row=2, column=1, columnspan=4, sticky=W+E)

            self.answer1Label.grid(row=4, column=0, sticky=W+E)
            self.answer2Label.grid(row=5, column=0, sticky=W+E)
            self.answer3Label.grid(row=6, column=0, sticky=W+E)
            self.answer4Label.grid(row=7, column=0, sticky=W+E)

            self.answer1Entry.grid(row=4, column=1, columnspan=4, sticky=W+E)
            self.answer2Entry.grid(row=5, column=1, columnspan=4, sticky=W+E)
            self.answer3Entry.grid(row=6, column=1, columnspan=4, sticky=W+E)
            self.answer4Entry.grid(row=7, column=1, columnspan=4, sticky=W+E)

            self.correctAnswerLabel.grid(row=9, column=0, sticky=W+E)
            self.correctAnswer1.grid(row=9, column=1, sticky=W+E)
            self.correctAnswer2.grid(row=9, column=2, sticky=W+E)
            self.correctAnswer3.grid(row=9, column=3, sticky=W+E)
            self.correctAnswer4.grid(row=9, column=4, sticky=W+E)

            self.pointsLabel.grid(row=11, column=0, sticky=W+E)
            self.pointsEntry.grid(row=11, column=1, columnspan=4, sticky=W+E)

            self.penaltyLabel.grid(row=12, column=0, sticky=W+E)
            self.penaltyEntry.grid(row=12, column=1, columnspan=4, sticky=W+E)

            self.explanationLabel.grid(row=14, column=0, sticky=W+E)
            self.explanationEntry.grid(row=14, column=1, columnspan=4, sticky=W+E)

            self.nextButton.grid(row=17, column=0, columnspan=3, sticky=W+E)

            self.finishedButton = Button(self.parent, text="Finished", command=self.finish)
            self.finishedButton.grid(row=17, column=3, columnspan=2, sticky=E+W)

            self.parent.grid_rowconfigure(0, weight=1)
            self.parent.grid_rowconfigure(1, weight=1)
            self.parent.grid_rowconfigure(2, weight=1)
            self.parent.grid_rowconfigure(3, weight=1)
            self.parent.grid_rowconfigure(4, weight=1)
            self.parent.grid_rowconfigure(5, weight=1)
            self.parent.grid_rowconfigure(6, weight=1)
            self.parent.grid_rowconfigure(7, weight=1)
            self.parent.grid_rowconfigure(8, weight=1)
            self.parent.grid_rowconfigure(9, weight=1)
            self.parent.grid_rowconfigure(10, weight=1)
            self.parent.grid_rowconfigure(11, weight=1)

            self.parent.grid_columnconfigure(0, weight=1)
            self.parent.grid_columnconfigure(1, weight=1)
            self.parent.grid_columnconfigure(2, weight=1)
            self.parent.grid_columnconfigure(3, weight=1)
            self.parent.grid_columnconfigure(4, weight=1)

    def next(self):
        if self.title is "":
            widget = self.titleEntry
            title = widget.get()

            if title is "":
                return

            self.title = title
            self.parent.title(self.title)
            self.initUI()
        else:
            question = self.questionEntry.get()
            category = self.categoryEntry.get()
            points = self.pointsEntry.get()
            penalty = self.penaltyEntry.get()
            explanation = self.explanationEntry.get()

            answers = [self.answer1Entry.get(), self.answer2Entry.get(), self.answer3Entry.get(), self.answer4Entry.get()]

            if question is "" or category is "" or points is "" or penalty is "" or explanation is "":
                messagebox.showerror("Please fill in all fields!", "All fields must be filled in.")
                return

            for value in answers:
                if value is "":
                    messagebox.showerror("Please fill in all fields!", "All fields must be filled in.")
                    return

            question = {"category":category, "question":question, "answers":answers, "correct":self.correctAnswer.get(), "points":points, "penalty":penalty, "explanation":explanation}
            self.questions.append(question)

            self.categoryEntry.delete(0, END)
            self.questionEntry.delete(0, END)
            self.answer1Entry.delete(0, END)
            self.answer2Entry.delete(0, END)
            self.answer3Entry.delete(0, END)
            self.answer4Entry.delete(0, END)
            self.pointsEntry.delete(0, END)
            self.penaltyEntry.delete(0, END)
            self.explanationEntry.delete(0, END)

    def finish(self):
        tools = TriviaTools()

        file = tools.open_file(self.title+".trv", "w")

        lines = []

        lines.append(self.title+"\n")

        for question in self.questions:
            lines.append(question["category"]+"\n")
            lines.append(question["question"]+"\n")

            for answer in question["answers"]:
                lines.append(answer+"\n")

            lines.append(question["correct"]+"\n")
            lines.append(str(question["points"])+"\n")
            lines.append(str(question["penalty"])+"\n")
            lines.append(question["explanation"]+"\n")

        file.writelines(lines)
        file.close()

        self.parent.destroy()

0 个答案:

没有答案