Quantcast

Display in a text field using tkinter

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Display in a text field using tkinter

adedoyin adegoke
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  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Display in a text field using tkinter

Alan Gauld

"adedoyin adegoke" <[hidden email]> wrote >    def
create_widgets(self):
>        Label(self, text = "Database Name:").grid(...
>        self.txt_box = Entry(self, text = "hool").grid(...

The grid(and pack etc)  method returns None.
You have to create the widgets then apply the layout manager on a separate
line

       self.txt_box = Entry(self, text = "hool")
       self.txt_box.grid(....)

>
> Exception in Tkinter callback
>    self.txt_box.insert(END,trt)
> AttributeError: 'NoneType' object has no attribute 'insert'
>
> How can i correct this?

Don;t use grid() ion the same line as you create the widget.

HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 


_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Display in a text field using tkinter

James-2
In reply to this post by adedoyin adegoke


On Fri, Apr 2, 2010 at 2:25 AM, adedoyin adegoke <[hidden email]> 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  -  [hidden email]
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)







_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Display in a text field using tkinter

adedoyin adegoke
Thanks
I made the changes and the error was still there

"print self.favorite.get()" prints the name of the selected database on the console


From: James Reynolds <[hidden email]>
To: adedoyin adegoke <[hidden email]>
Cc: [hidden email]
Sent: Fri, April 2, 2010 2:26:36 PM
Subject: Re: [Tutor] Display in a text field using tkinter



On Fri, Apr 2, 2010 at 2:25 AM, adedoyin adegoke <[hidden email]> 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  -  [hidden email]
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)








_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Display in a text field using tkinter

Alan Gauld

"doyin adegoke" <[hidden email]> wrote

> I made the changes and the error was still there
>
> "print self.favorite.get()" prints the name of the selected database on
> the console

The problem is that you are still calling grid when you create the widget.
grid() returns None so all your self.xxx attributes are set to None.

You must call grid after you create the widget.


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 


_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Display in a text field using tkinter

adedoyin adegoke
In reply to this post by adedoyin adegoke
Thanks guys I was able to resolve it by changing

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

to

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

and

 self.txt_box.insert(END,trt)

to

 self.txt_box.insert(0,trt)

but i added

 self.txt_box.delete(0, END)

before inserting




From: James Reynolds <[hidden email]>
To: adedoyin adegoke <[hidden email]>
Sent: Sun, April 4, 2010 9:35:37 PM
Subject: Re: [Tutor] Display in a text field using tkinter

Did you try setting this (in the init):

self.create_widgets()

to something like this:

my_object = self.create_widgets()

?

On Fri, Apr 2, 2010 at 2:25 AM, adedoyin adegoke <[hidden email]> 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  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor




_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Loading...