首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python tkinter - 通过按下按钮,从加载到文本小部件中的数据库中更改值

Python tkinter - 通过按下按钮,从加载到文本小部件中的数据库中更改值
EN

Stack Overflow用户
提问于 2018-06-14 06:20:52
回答 1查看 0关注 0票数 0

我在使用一个数据库,目前正在创建的GCSE计算机科学选择题测验sqlitetkinter。在创建测验时,我遇到了如何在用户按下“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
EN

回答 1

Stack Overflow用户

发布于 2018-06-14 16:20:31

要更新Labeltkinter中的小部件,你应该textvariable在小部件声明后指定一个小部件。这意味着事先你需要声明一个StringVar,这是tkinter在小部件中处理类型的方式。以下是一个例子:

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

现在编辑并访问文本,使用以下功能:

str.set("whatever")
str.get()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100005386

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档