Hi all I have written code there are two definition functions but the only way I can get the whole thing to work is to put some code outside the definitions. Does anyone know how to fix the code so that I can call it from another module.
Please help
import Tkinter as tk # gives tk namespace
import csv
def hello():
print 'TO BE COMPLETED'
def get_list(event):
"""
function to read the listbox selection
and put the result in an 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, 1000)
# now display the selected text
#enter1.insert(0, hours[int(index)])
data = hours[int(index)]
for i in range(0, len(data)):
enter1.insert(tk.END, data[i])
#enter1.insert(tk.END, index)
# create the sample data file
f = open("timeline2.csv", "r")
line1 = f.readline()
data = line1.split(',')
st = data[0].split()
time = st[1].split(':')
stTime = time[0]
endTime = str(int(time[0]) + 1)
lines = f.readlines()
hours= []
x = 0
j = len(lines) - 2
while x < j:
currHour = []
while x < j:
line = lines[x]
currline = line.split(',')
if currline[0].find(' ' + stTime) != -1 or currline[0].find(' ' + endTime ) != -1:
currHour.append(line)
x += 1
else:
#print x
break
hours.append(currHour)
if int(stTime) > 24:
stTime = str(0)
else:
stTime = str( int(stTime) + 1)
if int(endTime) > 24:
endTime = str(0)
else:
endTime = str( int(endTime) + 1)
root = tk.Tk()
root.title("Timeline Generator Results")
root.geometry("640x510")
menubar = tk.Menu(root)
# create a pulldown menu, and add it to the menu bar
filemenu = tk.Menu(menubar, tearoff=0)
filemenu.add_command(label="Open", command=hello)
filemenu.add_command(label="Save", command=hello)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)
# create more pulldown menus
editmenu = tk.Menu(menubar, tearoff=0)
editmenu.add_command(label="Cut", command=hello)
editmenu.add_command(label="Copy", command=hello)
editmenu.add_command(label="Paste", command=hello)
menubar.add_cascade(label="Edit", menu=editmenu)
helpmenu = tk.Menu(menubar, tearoff=0)
helpmenu.add_command(label="About", command=hello)
menubar.add_cascade(label="Help", menu=helpmenu)
# display the menu
root.config(menu=menubar)
# create the listbox (note that size is in characters)
listbox1 = tk.Listbox(root, width=15, height=6)
listbox1.grid(row=0, column=0, ipadx=5, ipady=5)
# 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)
enter1 = tk.Listbox(root, width=150, height=6)
enter1.grid(row=0, column=3)
zscroll = tk.Scrollbar(command=enter1.yview, orient=tk.VERTICAL)
zscroll.grid(row=0, column=4, sticky=tk.N+tk.S)
enter1.configure(yscrollcommand=zscroll.set)
# load the listbox with data
print "%i sub-lists in hours[]" % len(hours)
for i in range(0, len(hours)):
print "length of hours[{0}] = {1}".format(i,len(hours[i]))
listbox1.insert(tk.END, len(hours[i]))
# left mouse click on a list item to display selection
listbox1.bind('<ButtonRelease-1>', get_list)
f = open("timeline2.csv", "r")
reader = csv.reader(f)
listbox2 = tk.Listbox(root, width=150, height=10)
listbox2.grid(row=3, columnspan=100,pady=20, ipadx=60, ipady=20)
# create a vertical scrollbar to the right of the listbox
xscroll = tk.Scrollbar(command=listbox2.yview, orient=tk.VERTICAL)
xscroll.grid(row=3, column=7, pady=20,sticky=tk.N+tk.S)
listbox2.configure(yscrollcommand=xscroll.set)
for row in reader:
i = i + 1
#listbox2.insert(tk.END, i)
listbox2.insert(tk.END, row)
#listbox2.insert(tk.END, "\n")
listbox2.yview(tk.MOVETO, 1.0)
root.mainloop()