我在使用一个数据库,目前正在创建的GCSE计算机科学选择题测验sqlite
和tkinter
。在创建测验时,我遇到了如何在用户按下“Next Question”按钮时能够更改“Question”变量的值,以便将新问题作为旧问题加载到文本窗口小部件中已移除。
代码:
class IssuesMCQ(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Welcome to the Issues MCQ Page.", font=LARGE_FONT)
label.pack(pady=10,padx=10)
#The "pady" command is to add external padding to the window on the y axis
#The "padx" command is to add external padding to the window on the x axis
connect = lite.connect("MCQ.db")#Tells the program what database to connect to
c = connect.cursor()#Connects the program with the database and opens it
T = Text(self, height=20, width=100)#Creates a Text Box to store the Mark Scheme in
S = Scrollbar(self)#Creates A scrollbar to scroll through the Text Box
MCQE = Entry(self)
T.pack()#Packs the Text Box with the default Settings
S.pack(side=RIGHT, fill=Y)
#The Option "side=RIGHT" means the program will pack the scrollbar on the right hand side of the window
#The Option "fill=Y" means that the program will make sure that the scrollbar will fill the window across the y axis
MCQE.pack()
S.config(command=T.yview)#Configures the Scrollbar telling the program what the scrollbar will control
T.config(yscrollcommand=S.set)#Configures the Scrollbar telling it that the Scrollbar is set to move it
global Question
Question = 1
c.execute("SELECT Question FROM Questions WHERE QuestionID= ?", (Question,))
for i in c.fetchall():
text = i[0]
T.insert(END, text)
button = ttk.Button(self, text="Check Your Answer",
command=lambda: CheckAnswers(MCQE, Question))
button.pack()
button1 = ttk.Button(self, text="Next Question",
command=lambda: NextQuestion())
button1.pack()
def CheckAnswers(MCQE, Question):
connect = lite.connect("MCQ.db")#Tells the program what database to connect to
c = connect.cursor()#Connects the program with the database and opens it
Answer = MCQE.get()
QCA = 1
c.execute("SELECT CorrectAnswer FROM CorrectAnswers WHERE CAID= ?", (QCA,))
for i in c.fetchall():
CA = i[0]
if Answer == CA:
messagebox.showinfo("Checking Your Answer", "Your Answer is Correct")
else:
messagebox.showerror("Checking Your Answer", "Your Answer is Incorrect")
QCA = QCA + 1
def NextQuestion():
global Question
Question = Question + 1
发布于 2018-06-14 16:20:31
要更新Label
tkinter中的小部件,你应该textvariable
在小部件声明后指定一个小部件。这意味着事先你需要声明一个StringVar
,这是tkinter在小部件中处理类型的方式。以下是一个例子:
str = StringVar()
lb = Label(self, textvariable=str)
现在编辑并访问文本,使用以下功能:
str.set("whatever")
str.get()
https://stackoverflow.com/questions/-100005386
复制相似问题