[Tutor] Clickable listbox opening specific .txt files

Alan Gauld alan.gauld at btinternet.com
Thu Jan 14 04:03:59 EST 2016


On 14/01/16 08:05, Ali Moradi wrote:
> Hi, i want to open specific .txt files when each item on the listbox was
> clicked! When i click on the first item for example it opens 1.txt and
> shows it on the text widget on the right, and when i select the second item
> on the list first it erases the text on the Text widget and then opens
> 2.txt file and so on till item 17 on the list.

So what does happen?

> Please modify my code i'm still beginner
> Thanks a lot
> 
> paste.pound-python.org/show/uMUswu4YSmMY5XBPPgIy/

We won't write your code for you but we will point out areas
for improvement. But... see below.

For the future:
It's best if you post the actual code in your mail
(using plain text please). We are happy to accept up
to, say, a hundred lines.

Also post full error messages, if any.

Finally tell us the OS and Python version.

Here is a slightly shortened version of your code:

########################
from Tkinter import *

root = Tk()
root.title("Renkontoj kun Diferenculoj")
root.iconbitmap(r"C:\python27\DLLs/py.ico")
root.resizable(0, 0)
frame1 = LabelFrame(root, height=300, width=400, text='Lecionoj')
frame2 = LabelFrame(root, height=300, width=400, text='Tekstoj')
frame1.grid(row=0, column=0)
frame2.grid(row=0, column=1)

def click(event):
    index = list.curselection()[0]
    file = open(r"C:\Users\deadmarshal\PycharmProjects\ali\1.txt").read()

list = Listbox(frame1)
list.insert(1, "Konatiĝu kun Kamila")
list.insert(2, "Sinjoro Johano")
list.insert(3, "Onklino Marta")
scroll = Scrollbar(frame1, orient=VERTICAL, command=list.yview)
scroll.grid(row=0, column=2, sticky='ns')
list.grid(row=0, column=1)
list.bind("<ButtonRelease-1>", click)
text = Text(frame2).grid()

root.mainloop()
############################

Notice that your click function does not do anything. It reads
the file then throws it away when the function exits. You need
to insert the file data into your text widget. (And probably
clear the text widget first.)

Finally, note that by using the name liust you are hiding a builtin
Python function, so you will no longer be able to convert things
to a list using list(). This doesn't mattter just now but may be
a problem later. Its best to avoid using names that Python already
uses. One way to do that is to add a descriptive modifier like
name_list, say.

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