[Tutor] Display in a text field using tkinter

adedoyin adegoke doyennehoney at yahoo.com
Fri Apr 2 08:25:47 CEST 2010


from Tkinter import *
import MySQLdb

class Snoop(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        Label(self, text = "Database Name:").grid(row = 0, column = 0,
sticky = W)
        self.txt_box = Entry(self, text = "hool").grid(row = 0, column = 1,
sticky = W)
        Button(self, text = "Submit", command = self.connect_db).grid(row =
1, column = 1, sticky = W)
        Label(self, text = "Tables:").grid(row = 2, column = 0, sticky = W)

        Label(self, text = "Information:").grid(row = 2, column = 1, sticky
= W)
       # self.txt = Text(self, width = 40, height = 5, wrap = WORD).grid(row
= 3, column = 1, sticky = W)

    def connect_db(self):
        db= MySQLdb.connect(host="localhost", user="root" , passwd="")
        cursor = db.cursor()
        cursor.execute("show databases")

        self.favorite = StringVar()

        result = cursor.fetchall()
        i = 4
        for record in result:
            Radiobutton(self,
                     text = record,
                     variable = self.favorite,
                     value = record,
                     command = self.update_text
                     ).grid(row = i, column = 0, sticky = W)
            i+=1

        #print database
        #self.txt.delete(0.0, END)
        #self.get_db(database)
    def update_text(self):
        print self.favorite.get()
        trt = self.favorite.get()
        self.txt_box.insert(END,trt)



#db.close
root = Tk()
root.title("Snoop")
start = Snoop(root)

root.mainloop()



The above code will snoop and fetch all d available databases using tkinter.
When I select one of these databases, the name should be inserted in a text
field instead it throws the following error ;

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 1413, in __call__
    return self.func(*args)
  File "/home/NetBeansProjects/python/src/Xsnoop.py", line 45, in
update_text
    self.txt_box.insert(END,trt)
AttributeError: 'NoneType' object has no attribute 'insert'


How can i correct this?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100402/a4b124ed/attachment-0001.html>


More information about the Tutor mailing list