无法使用tkinter在测验中增加sqlite数据库中的问题

时间:2017-12-31 21:33:39

标签: python python-3.x sqlite tkinter

我还在研究我的测验计划,但我目前遇到的问题是让程序增加从我也拥有的数据库中检索到的问题。当我尝试移动到我的数据库中的下一条记录时,我无法真正更改页面的内容,一切都保持不变,我到目前为止还没有能够纠正这个问题。我觉得好像使用变量是一个问题,但我无法想到这个问题。这是我的代码部分,我遇到了这个问题:

class ques(tk.Frame):
def __init__(self, parent, controller):
    tk.Frame.__init__(self, parent)

    fetchrecNum = "SELECT MAX(qnumber) FROM questions"
    cursor.execute(fetchrecNum)
    self.recNum=str(cursor.fetchall())
    self.recNum=self.recNum.strip("[('")
    self.recNum=self.recNum.strip("',)]")
    self.recNum=int(self.recNum)
    recordNum = tk.Label(self, text = self.recNum)
    recordNum.pack()

    self.Qn = 1
    self.quizScore = 0

    fetchQ = "SELECT questioncontent FROM questions WHERE qnumber=?"
    cursor.execute(fetchQ, [self.Qn])
    Q=str(cursor.fetchall())
    Q=Q.strip("[('")
    Q=Q.strip("',)]")
    question = tk.Label(self, text = Q)
    question.pack()

    fetchA1 = "SELECT qanswer1 FROM questions WHERE qnumber=?"
    cursor.execute(fetchA1, [self.Qn])
    A1=str(cursor.fetchall())
    A1=A1.strip("[('")
    A1=A1.strip("',)]")
    answer1 = tk.Label(self, text = A1)
    answer1.pack()

    fetchA2 = "SELECT qanswer2 FROM questions WHERE qnumber=?"
    cursor.execute(fetchA2, [self.Qn])
    A2=str(cursor.fetchall())
    A2=A2.strip("[('")
    A2=A2.strip("',)]")
    answer2 = tk.Label(self, text = A2)
    answer2.pack()

    fetchA3 = "SELECT qanswer3 FROM questions WHERE qnumber=?"
    cursor.execute(fetchA3, [self.Qn])
    A3=str(cursor.fetchall())
    A3=A3.strip("[('")
    A3=A3.strip("',)]")
    answer3 = tk.Label(self, text = A3)
    answer3.pack()

    fetchA4 ="SELECT qanswer4 FROM questions WHERE qnumber=?"
    cursor.execute(fetchA4, [self.Qn])
    A4=str(cursor.fetchall())
    A4=A4.strip("[('")
    A4=A4.strip("',)]")
    answer4 = tk.Label(self, text = A4)
    answer4.pack()

    fetchcA ="SELECT correctans FROM questions WHERE qnumber=?"
    cursor.execute(fetchcA, [self.Qn])
    self.cA=str(cursor.fetchall())
    self.cA=self.cA.strip("[('")
    self.cA=self.cA.strip("',)]")



def confirmAnswer(self):
    answerGiven = self.enterAnswer
    correctAnswer = self.cA
    if answerGiven == correctAnswer:
        self.rightOrWrong.configure(text ="Correct")
        self.quizScore = (self.quizScore + 1)
    else:
        self.rightOrWrong.configure(text="Incorrect")
    if self.Qn < self.recNum:
        self.Qn = (self.Qn+1)
        lambda: controller.show_frame(ques)
    else:
        self.rightOrWrong.configure(text="Quiz Complete! Your score was: " + str(self.quizScore))

我不知道我怎么能试着让页面的内容改变,但我希望我可以向其他人学习,因为我周围的人都没有帮助(包括老师),这是我有最好的选择。提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我无法测试它,但我会做类似代码的事情。

public class Personal_RecyclerAdapter extends RecyclerView.Adapter<Personal_RecyclerAdapter.MyHoder>{ List<Blog> list; List<Complainee> list2; Context context; public Personal_RecyclerAdapter(List<Blog> list, List<Complainee> list2 , Context context) { this.list = list; this.list2 = list2; this.context = context; } @Override public Personal_RecyclerAdapter.MyHoder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.individual_row_personalcomplaint,parent,false); Personal_RecyclerAdapter.MyHoder myHoder = new Personal_RecyclerAdapter.MyHoder(view); return myHoder; } @Override public void onBindViewHolder(final Personal_RecyclerAdapter.MyHoder holder, final int position) { final Blog mylist = list.get(position); Complainee mylist2 = list2.get(position); holder.Description.setText(mylist.getDescription()); holder.DateTime.setText(mylist.getDate() + " " + mylist.getTime()); holder.Complainee.setText(mylist.getComplainee_Name()); holder.Complainee.setText(mylist2.getName()); holder.Complain_Address.setText(mylist.getAddress()); holder.btn_inviteComplainant.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent invitecomplainant = new Intent(context,brgy_inviteComplainant.class); invitecomplainant.putExtra("Personal_ComplaintNo", mylist.getComplaintNo()); context.startActivity(invitecomplainant); } }); } @Override public int getItemCount() { int arr = 0; try{ if(list.size()==0){ arr = 0; } else{ arr=list.size(); } }catch (Exception e){ } return arr; } class MyHoder extends RecyclerView.ViewHolder{ TextView Description,DateTime, Complain_Address,ComplaintNo, Complainee; // ImageView MediaURL; RelativeLayout relLyaout; Button btn_inviteComplainant; public MyHoder(View itemView) { super(itemView); // ComplaintNo = (TextView) itemView.findViewById(R.id.textView_ComplaintNo); Description = (TextView) itemView.findViewById(R.id.complain_description); DateTime= (TextView) itemView.findViewById(R.id.complain_datetime); // MediaURL= (ImageView) itemView.findViewById(R.id.imageDisplay); Complainee = (TextView) itemView.findViewById(R.id.complain_complainee); Complain_Address = (TextView) itemView.findViewById(R.id.complain_address); btn_inviteComplainant = (Button) itemView.findViewById(R.id.btn_inviteComplainant); } } } 中,我创建空标签并执行分离的方法,该方法从数据库中读取数据并将其放入标签中。之后我可以使用这种方法来获得下一个问题。

要从数据库中获取一行,您可以使用__init__。要从行获取数据,您不必使用fetchone()str(),而是索引strip()row[0]等。如果您有列,则可能需要更改索引数据库中的不同顺序。

row[1]