Tkinter从功能将错误代码打印到标签

时间:2019-01-28 13:40:51

标签: mysql python-3.x tkinter

我正在python中尝试使用Tkinter和MySQL,但遇到了无法解决的问题。我所有的Tkinter代码都封装在类中,并且我有一个函数,该函数将关闭并在MySQL表中插入新行。我想要的是一旦此操作完成或遇到错误,标签上就会显示一条消息。

这是调用函数的按钮,它在一个名为“ LandingPage”的类中:

Page Header

Group Header#1

   Group Header#1a

   Group Header#1b

Group Header#2

Group Header#3
Group Header#4

Group Footer#4

Group Footer#3

Group Footer#2

Group Footer#1

    Group Footer #1a

    Group Footer #1b  --> this section contain subreport

    Group Footer #1c

这是它调用的代码:

# Button to take all the user input in the entry boxes and send it to
# the addClientToDB() function that will insert them into the database.
btnAddClient = ttk.Button(addClient, text="Add Client",
                              command=lambda:addClientToDB(self, tbContactName.get(), tbEmailAddr.get(), tbClientCompany.get()))
# Adding the button to the grid at 3, 1, and having it span 2 columns.
# Giving it 15px padding in x and 5px in y
btnAddClient.grid(row=3, column=1, columnspan=2, padx=15, pady=5)

很显然,您可以在该代码中看到我正在尝试使用.config()方法设置标签“ lblErrorCode”,但是当我运行该代码时,什么也没有发生。数据已插入,但标签没有任何反应。但是我确实将输出输出到终端。因此,作为测试,我有意尝试输入一个太长的字符串,这会在终端上产生此结果,但对Label却没有任何作用:

    # Function for adding clients to the database. It needs the name of the person,
    # the email address and the company to be parsed to it
    def addClientToDB(self, name, emailAddr, company):
        # Creating the Connection string for the INSERT INTO statement, and adding
        # the values to the end of the statement
        cnxString = "INSERT INTO clientDetails(contactName, emailAddr, clientCompany)" \
        "VALUES(%s, %s, %s)"
        # Adding the arguments to a list that will also be sent
        args = (name, emailAddr, company)

        # Sending the string and the arguments off to the sendSqlQuery(), and
        # putting anything that was returned into the "ans" variable
        ans = sendSqlQuery(cnxString, args)

        lblErrorCode.config(text=ans)
        # Printing anything that was returned to the console for debugging purposes
        print(ans)

关于如何获取更新标签功能的想法吗?

提前谢谢吗?

0 个答案:

没有答案