0

In my coding it will display the label from the values of mysql if the Entry box values are same.

It runs perfectly when the datas are same,the correct respective labels are displayed

Problem: When mysql data and entry box values not same,it show me error .And it also showing me wrong label name when no value is provided.

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1470, in __call__
    return self.func(*args)
  File "C:\Python27\labeling1.py", line 33, in retrieve_inpu
    self.label = tki.Label(self.txt_frm,text=row[1])
TypeError: 'NoneType' object has no attribute '__getitem__'

Database:

bike appartment lead
car  house      pen

Coding:

import Tkinter as tki
import MySQLdb
db = MySQLdb.connect(host="localhost", # your host, usually localhost
                         user="root", # your username
                          passwd="mysql", # your password
                          db="sakila") # name of the data base
cursor = db.cursor()

class App(object):
     def __init__(self,root):
         self.root = root

         self.txt_frm = tki.Frame(self.root, width=900, height=900)
         self.txt_frm.pack(fill="both", expand=True)
         button3 = tki.Button(self.txt_frm,text="CLICK 1", command = self.retrieve_inpu)
         button3.grid(column=0,row=2)
         self.entry = tki.Entry(self.txt_frm)
         self.entry.grid(column=1,row=0)

         #place holder for label variable
         self.label = None
         self.label1=None

     def retrieve_inpu(self):
        ent = self.entry.get()
        cursor.execute('SELECT A1,A2,A3 FROM adarsh1 WHERE A1=%s', (ent,))
        row = cursor.fetchone()
        #destroy the widget if it has been created
        #you will have a bunch of orphans if you don't
        if self.label:
            self.label.destroy()

        self.label = tki.Label(self.txt_frm,text=row[1])
        self.label.grid(column=0,row=3)

        if self.label1:
            self.label1.destroy()

        self.label1 = tki.Label(self.txt_frm,text=row[2])
        self.label1.grid(column=0,row=4)            


root = tki.Tk()
app = App(root)
root.mainloop()

1 Answer 1

2

Cursor.fetchone returns None if the query does not return any row. You need to handle that case.

row = cursor.fetchone()
if row is None:
    # no matching row - do something
    # `return` if necessary
Sign up to request clarification or add additional context in comments.

2 Comments

the errors are vanished,but the previous labels persists..how to make them not display?
@adsalila, Try to reset the text of the label (inside the if body): self.label['text'] = ''

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.