[Tutor] Display in a text field using tkinter

James Reynolds eire1130 at gmail.com
Fri Apr 2 15:26:36 CEST 2010


On Fri, Apr 2, 2010 at 2:25 AM, adedoyin adegoke <doyennehoney at yahoo.com>wrote:

> 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?
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
Is this line printing anything:

"print self.favorite.get()"?



I would try something like this:

Move this to the "__init__":

self.favorite = StringVar()


Change this:

self.txt_box = Entry(self, text = "hool").grid(row = 0, column = 1, sticky =
> W)


to this:


self.txt_box = Entry(self, text = self.name).grid(row = 0, column = 1,
> sticky = W)


in the "__init__" add two lines:


self.name = StringVar()
self.name.set("hool")

In update_text change this:

self.txt_box.insert(END,trt)


to this:

self.name.set(trt)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100402/10944d92/attachment.html>


More information about the Tutor mailing list