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

A simple way is this

import Tkinter as tk  # gives tk namespace
import csv

def hello():
    # etc
    
def get_list(event):
    # etc

def main():
    # put line 26 to 141 in this function

if __name__ == "__main__":
    # this will not be executed if your file is imported from another module
    main()

Hi Cheers for the reply but i actually tries that before the gui runs but it cannot see listbox1(which is created in that code) in any of the other functions

I think it would be better to use a class for your gui

class MyGui(object):

  def __init__(self):
    # put here the code from your previous lines 26 ... 141
    # and replace everywhere 'listbox1' by 'self.listbox1'
    # and 'root' by 'self.root', and get_list by self.getlist

  def hello(self):
    # etc

  def get_list(self, event):
    # replace 'listbox1' by 'self.listbox1' in this function's body

if __name__ = "__main__":
  MyGui().root.mainloop()

Edited 6 Years Ago by Gribouillis: n/a

That worked a treat, cheers. You have no idea how long I spent working on that :)

Could I ask you a question. If I have a listbox in Tkinter is there any way that I can search the listbox for a work or substring and Highlight that string in a colour? Or could I replace the listbox with something else like a canvas to do it.

That worked a treat, cheers. You have no idea how long I spent working on that :)

Could I ask you a question. If I have a listbox in Tkinter is there any way that I can search the listbox for a work or substring and Highlight that string in a colour? Or could I replace the listbox with something else like a canvas to do it.

Sorry, I don't know tkinter well enough. It's a question for Vegaseat :)

Thanks for all your help. Really appreciate it. Only starter python about a month and a half ago and picking it up as I go along.

I sent a message to Vegaseat to see if there are any suggestions.

Thanks again :)

This article has been dead for over six months. Start a new discussion instead.