python stringvar.get_Python StringVar get函数什么都不返回?

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

我有一个UI,它由多个标签和使用grid方法放置的输入框组成(可能不重要,但仍然如此)。还有4个单选按钮。当单击UI上的按钮(在本例中是Next按钮)时,相关输入框中的值将存储在字典中,然后字典会附加到类中的列表中。在

当我将值存储到字典中时,我的问题就出现了——我从附加到单选按钮的StringVar()中得到的值总是空的——它会出现一个空白字符串。在

有问题的班级在下面。我在调用next(self)函数中的get()方法。在

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

在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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值