-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistbox.py
executable file
·133 lines (108 loc) · 3.63 KB
/
listbox.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# http://www.daniweb.com/software-development/python/code/216827/working-with-a-tkinter-listbox-python#
import Tkinter as tk # gives tk namespace
import os
def directory_list():
dirlist = os.listdir("/usr")
from pprint import pprint
pprint(dirlist)
def add_item():
listbox1.insert(tk.END, enter1.get())
def delete_item():
try:
index = listbox1.curselection()[0]
listbox1.delete(index)
except IndexError:
pass
def get_list(event):
# reads the listbox selection and puts result in entry widget
# get selected line index
index = listbox1.curselection()[0]
# get the line's text
seltext = listbox1.get(index)
# delete previous text in enter1
enter1.delete(0, 50)
# now display the selected text
enter1.insert(0, seltext)
def set_list(event):
# inserts an edited line
try:
index = listbox1.curselection()[0]
# delete old listbox line
listbox1.delete(index)
except IndexError:
index = tk.END
# insert edited item back into listbox1 at index
listbox1.insert(index, enter1.get())
def sort_list():
temp_list = list(listbox1.get(0, tk.END))
temp_list.sort(key=str.lower)
# delete contents of present listbox
listbox1.delete(0, tk.END)
# load listbox with sorted data
for item in temp_list:
listbox1.insert(tk.END, item)
def save_list():
# get a list of listbox lines
temp_list = list(listbox1.get(0, tk.END))
# add a trailing newline char to each line
temp_list = [chem + '\n' for chem in temp_list]
# give the file a different name
fout = open("chem_data2.txt", "w")
fout.writelines(temp_list)
fout.close()
# create the sample data file
str1 = """ethyl alcohol
ethanol
ethyl hydroxide
hydroxyethane
methyl hydroxymethane
ethoxy hydride
gin
bourbon
rum
schnaps
"""
fout = open("chem_data.txt", "w")
fout.write(str1)
fout.close()
# read the data file into a list
fin = open("chem_data.txt", "r")
chem_list = fin.readlines()
fin.close()
# strip the trailing newline char
chem_list = [chem.rstrip() for chem in chem_list]
root = tk.Tk()
root.title("Listbox Operations")
# create the listbox (note that size is in characters)
listbox1 = tk.Listbox(root, width=50, height=6)
listbox1.grid(row=0, column=0)
# create a vertical scrollbar to the right of the listbox
yscroll = tk.Scrollbar(command=listbox1.yview, orient=tk.VERTICAL)
yscroll.grid(row=0, column=1, sticky=tk.N+tk.S)
listbox1.configure(yscrollcommand=yscroll.set)
# use entry widget to display/edit selection
enter1 = tk.Entry(root, width=50, bg='yellow')
enter1.insert(0, 'Click on an item in the listbox')
enter1.grid(row=1, column=0)
# pressing the return key will update edited line
enter1.bind('<Return>', set_list)
# or double click left mouse button to update line
enter1.bind('<Double-1>', set_list)
# button to sort listbox
button1 = tk.Button(root, text='Sort the listbox ', command=sort_list)
button1.grid(row=2, column=0, sticky=tk.W)
# button to save the listbox's data lines to a file
button2 = tk.Button(root, text='Save lines to file', command=save_list)
button2.grid(row=3, column=0, sticky=tk.W)
# button to add a line to the listbox
button3 = tk.Button(root, text='Add entry text to listbox', command=add_item)
button3.grid(row=2, column=0, sticky=tk.E)
# button to delete a line from listbox
button4 = tk.Button(root, text='Delete selected line ', command=delete_item)
button4.grid(row=3, column=0, sticky=tk.E)
# load the listbox with data
for item in chem_list:
listbox1.insert(tk.END, item)
# left mouse click on a list item to display selection
listbox1.bind('<ButtonRelease-1>', get_list)
root.mainloop()