我正在用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()

python stringvar.get_Python StringVar get函数什么都不返回?相关推荐

  1. python全栈开发笔记---------函数

    一 数学定义的函数与python中的函数 初中数学函数定义:一般的,在一个变化过程中,如果有两个变量x和y,并且对于x的每一个确定的值,y都有唯一确定的值与其对应,那么我们就把x称为自变量,把y称为因 ...

  2. python中比较重要的几个函数_Python 几个重要的内置函数 python中的内置函数和关键字需要背过吗...

    python重要的几个内置函数用法 python内置函数什么用忘不掉的是回忆,继续的是生活,错过的,就当是路过吧.来来往往身边出现很多人,总有一个位置,一直没有变.看看温暖的阳光,偶尔还是会想一想. ...

  3. 最全Python函数总结和应用(超详细+建议收藏),基本所有内置函数,心得都在这了,踩的坑也在里面了,最后还有函数的魂

    希望能帮助到你 前言 基础的函数了解 惊喜类 all() 和 any() lambda函数 sorted()函数 map()函数 filter()函数 reduce()函数 eval()函数 zip函 ...

  4. 为什么 Python 不支持函数重载?其他函数大部分都支持的?

    为了考虑为什么 python 不提供函数重载,首先我们要研究为什么需要提供函数重载. 函数重载主要是为了解决两个问题. 1.可变参数类型. 2.可变参数个数. 另外,一个基本的设计原则是,仅仅当两个函 ...

  5. Python所有的内置函数 , 都帮你整理好了!

    来源:Python编程与实战 1. abs() 语法 abs(x), 返回一个数的绝对值.参数可以是一个整数或浮点数.如果参数是一个复数,则返回它的模 示例 2. all() 语法 all(itera ...

  6. 30 段极简 Python 代码:这些小技巧你都 Get 了么?

    选自 | towardsdatascienc 编译 | 机器之心 学 Python 怎样才最快,当然是实战各种小项目,只有自己去想与写,才记得住规则.本文是 30 个极简任务,初学者可以尝试着自己实现 ...

  7. python常用的内置函数

    内置函数,就是Python提供的, 可以直接拿来直接用的函数. 一.数字相关 01 数据类型 bool() 描述:测试一个对象是True, 还是False.bool 是 int 的子类. 语法:cla ...

  8. 教你如何运用python实现简单文件读写函数

    这篇文章主要为大家详细介绍了python实现简单文件读写函数,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 python作为脚本性语言,加上它的简便易用性.会经常当作脚 ...

  9. python绘制条形图用什么函数_Python绘制正余弦函数图像完整代码

    通过python绘制正弦和余弦函数,从默认的设置开始,一步一步地调整改进,让它变得好看,变成我们初高中学习过的图象那样.通过这个过程来学习如何进行对图表的一些元素的进行调整. 01. 简单绘图 mat ...

最新文章

  1. 探索客户端JavaScript
  2. Paket:一个面向.NET的包管理器
  3. Idea创建简单Java Web项目并部署Servlet
  4. 数据结构之基于Java的顺序栈实现
  5. 8051、ARM、AVR
  6. redis 入门总结
  7. Java递归算法经典实例
  8. 天野学院易语言模拟脚本制作
  9. MS08067红队攻防班 第五期开班啦!(2021年最后一期)
  10. 常见B端产品经理面试问题及答案(一)
  11. CAD打断线条的快捷键是什么?CAD打断线条教程
  12. 企业域名注册手续_如何注册域名以及ICP备案
  13. 解决新版本谷歌浏览器CROS跨域问题
  14. 2020年最新as中jni----------动态注册
  15. 安森美推出ecoSpin系列,重新定义无刷直流电机控制
  16. 显示网站统计量和访客地图
  17. 如何读博士-2021.06.12
  18. 高等数学上学习总结(集合,邻域,函数)
  19. ps更换证件照底色(视频版)
  20. ConcurrentHashMap是如何保证线程安全的?

热门文章

  1. Python re模块将字符串分割为列表
  2. soupUI生成webservice客户端代码
  3. js解析xml字符串或xml文件,将其转换为xml对象方法
  4. 关于链表逆置的递归和迭代方法
  5. android 右边抽屉,android – 抽屉在右侧抽屉中切换
  6. Xshell 鼠标选中 中断ctrl+c 问题
  7. 湖南省第六届大学生计算机程序设计竞赛---数字整除
  8. [Z]四种浏览器对 clientHeight、offsetHeight、scrollHeight、clientWidth、offsetWidth 和 scrollWidth 的解释...
  9. FreeBSD 安装axel提高ports的安装速度
  10. 信息系统项目管理通关指南