Discussion:
[Tutor] clickable listbox with Tkinter (python2)
Ali Moradi
2015-06-18 14:13:56 UTC
Permalink
Hi, i studied your book that u said before.

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

http://pastebin.com/GpWc8Pnw

now, i've got a better idea! , i can use two lists like this code (
http://pastebin.com/sJj39UjA), just the items should be loaded from my
database. like when i click water for example in the first list, the word
akvo in the second list appears.

now what is the method for filling the listbox from sqlite ? please modify
the code for me becuz i'm beginner.
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-06-18 17:16:56 UTC
Permalink
Post by Ali Moradi
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.
Post by Ali Moradi
#--------------------------------
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!
Post by Ali Moradi
cur = db.cursor()
cur.execute('Select * from vortoj')
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.
Post by Ali Moradi
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


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Loading...