[Tutor] clickable listbox with Tkinter (python2)

Alan Gauld alan.gauld at btinternet.com
Thu Jun 18 19:16:56 CEST 2015


On 18/06/15 15:13, Ali Moradi wrote:

> now i came up with this code, which i can't now show my words in the list
> box! ?

 > root = Tk()
 > l = Listbox(root, height=10)
 > s = Scrollbar(root, orient=VERTICAL)
 > l.configure(yscrollcommand=s.set)
 > l.pack()
 > root.mainloop()


I'd leave out the scrollbar for the moment. Keep it as simple
as possible then add features once the simple bits are working.
The more features you add the more there is to go wrong and
you're never quite sure which bit is faulty. If you only add
new features after the rest work then you know the new bit
is what is broken.

 > #--------------------------------
 > db = sqlite.connect('/home/deadmarshal/Desktop/Tkinter')

That's probably a bad name for your database file.
It should probably be called something like word_dictionary.db...
Its also usually best to keep databases in a folder rather
than on your desktop but that's just to avoid mess on your screen!

 > cur = db.cursor()
 > cur.execute('Select * from vortoj')
 > for row in cur:
 >    print(row)

This section has nothing to do with the GUI.
But does it work? Are you seeing the rows printed on the console?

You can store the rows in a variable using the

cur.fetchall() function.

You can then convert them into strings for display
(via print or in the GUI). You should get a list
of tuples so you can build a list of strings with something like:

rows = cur.fetchall()
display_rows = []
for row in rows:
     display_rows.append(str(row[0] + ' ' + str(row[1]) )

which can be shortened using a list comprehension to:

display_rows = [str(row[0] + ' ' + str(row[1]) for row in rows]

You can then insert these strings into your GUI as I did
in my original example.


> now, i've got a better idea!

Get the first idea working as its easier.
Then you can add the extras.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list