Python tkinter - Change value from a database loaded into a text widget by the press of a button

I am currently creating a multiple choice question quiz for GCSE computer science using a database in sqlite and tkinter. While creating the quiz I have got stuck on how I would be able to change the value of the "Question" variable when the user will press the "Next Question" button so that a new question is loaded into the text widget as the old one is removed. Any help is appreciated, Thanks.

Code:

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
pythontkintersqlite3

Answers

answered 1 week ago SamBG #1

To update a Label widget in tkinter, you should assign a textvariable upon the declaration of the widget. This means that beforehand you will need to declare a StringVar, which is how tkinter handles types in widgets. Following is an example:

str = StringVar()
lb = Label(self, textvariable=str)

Now to edit and access the text, use the following functions:

str.set("whatever")
str.get()

For future reference, you will want to limit what you put in your question to the bare minimum necessary. It should be much more clear what you are asking and what the direct issue is. Cheers

comments powered by Disqus