[Tutor] making a list in Tkinter with clickable items (Python 2.x)

Alan Gauld alan.gauld at btinternet.com
Thu Apr 16 18:12:49 CEST 2015


On 16/04/15 11:00, Ali Moradi wrote:
> Hi, i want to load a bunch of words from my database into the listbox in
> Tkinter, make them clickable, and when i clicked on them the specific
> meaning of that word apears in a textbox beside the listbox.
>
> i have a code but it's not complete. i'm a beginner in Python :(
>

Focus on getting your UI working first. There are lots of problems 
below. Sort those then you can focus on the sing;e problem of managing 
the data, followed by the next single problem of clicking on an item to 
do something.

For now get your foundation working.

> code:
> #!/bin/python
> from tkinter import *
> from tkinter import ttk

If its Python v2 then you spell it Tkinter (capital T).
And ttk lives at the top level in Python 2 not under Tkinter.


> def z():
>      global entry
>      if(lb.get(lb.curselection())):
>          txt.insert(END, lb.get(lb.curselection()))

Please use descriptive names for functions, it makes
reading the code much easier.

Why use global entry when you don't use entry in your function?

Also its better practice to use a variable to store the current 
selection rather than ask for it twice:

def transferCurrentSelection():
     sel = lb.curselection()
     content = lb.get(sel)
     if content:
        txt.insert(END, content)

Its a lot easier to debug if things go wrong. And you can
more easily tweak it to become a reusable function too...

> root = Tk()
>
> entry = ttk.Entry(root);entry.grid(row = 0, column = 0)

Don't use semi-colons like that, it confuses the code and
is very hard to see and differentiate from a period.
Nobody will shout at you for using two lines.

> lb = Listbox(root)
>
> a=lb.insert(1, "crime")
> b=lb.insert(2, "murder")
> lb.insert(3, "get")
> lb.insert(4, "take")

Why store a and b but not the other two results?
In fact insert() returns None anyway so storing
it is pointless.

> Button(root, text="BUtt", command=z).grid()
>
> lb.grid(row = 1, column = 0)

Its usually better to keep the geometry manager lines
beside the widget creation lines. It doesn't hurt if
you pack/grid the widget before populating it.

> scroll = Scrollbar(root, orient = VERTICAL, command = lb.yview)
> scroll.grid(row = 0, column = 1, sticky = 'ns')
> lb.config(yscrollcommand = scroll.set)

You could have set this when creating the Listbox.
Having random lines of code hidden among other widget code
makes debugging much harder.

> txt = Text(root, width = 60, height = 30,)
> txt.grid(row = 1, column = 2)
>
> root.mainloop()

Once you get this working we can revisit the matter of
retrieving data from a database, populating the list box
and then associating the clicked result with a meaning
(from the database presumably?).


-- 
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