2

Tkinter beginner here, and general python noob.

I have a program that appends items to a list and displays that list in a listbox. It'll display all items properly upon reload, but I want the new values to be visible in the listbox without having to reload. Here's my attempt. It doesn't return any error, but it doesn't work either.

I'm running 2.7 on Mac OSX. Remember, I'm fairly new at this stuff.

def OnPressEnter(self,event):
    hold = self.entryVariable.get()
    hold = str(hold)
    with open('GoalTrak/StudentList.txt', 'a') as textfile: #This appends students to the already existing document of students
        if (hold) not in student_list_temp:
            student_list_temp.append(hold[:-1])
            textfile.write(hold + '\n')
            self.labelVariable.set( self.entryVariable.get() + " added" )
            self.StudentListDisplay.insert(Tkinter.END,hold[:-1])
        else:
            self.labelVariable.set( self.entryVariable.get() + " is already in list" )
        textfile.close()
    self.entry.focus_set()
    self.entry.selection_range(0, Tkinter.END)

Thanks in advance!

EDIT: Here is the code in which I define my listbox:

self.StudentListDisplay = Tkinter.Listbox(self)
    self.StudentListDisplay.grid(row=2,column=0,columnspan=2,sticky='W')
    for student in student_list_temp:
        self.StudentListDisplay.insert(Tkinter.END, student)
5
  • Can you show us the code with which you define your ListBox? Commented Mar 28, 2014 at 18:03
  • what do you mean by "without having to reload"? Commented Mar 28, 2014 at 18:13
  • @mgilson The only way the listbox display updates is if I quit the program and reboot it. Commented Mar 28, 2014 at 18:17
  • How do you know it doesn't work? Have you verified that the code that adds variables to the list is being called? Certainly it's possible to insert items in a listbox at any time, so if it's not working, maybe that code is never being called? Commented Mar 28, 2014 at 18:32
  • Can you show us what calls OnPressEnter? is it a binding? A button command? Something else? Commented Mar 28, 2014 at 18:33

1 Answer 1

2

One method that I use is to delete everything in the list box and put the new list in. For instance

listbox.delete(0, END)
for i in range(len(new_list)):
    listbox.insert(END,new_list[i][0])

This deletes everything currently in the box and puts the new list in.

Hope this helped, and comment if you have any questions.

Sign up to request clarification or add additional context in comments.

7 Comments

I'm not sure but I have a couple ideas. Try removing the ':' from hold[:-1]. This I think is your problem. It has nothing to do with what I posted earlier. Let me know if this works and I will edit my answer to reflect the correct answer with a little more explaination.
it still works but wouldn't that be specifying an impossible start position? Also, it didn't fix my original problem.
list[:-1] returns the entire list without the last element. list[-1] returns just the last element. Did you do it for all the holds in that section?
One more thing. You can remove the textfile.close() line it is unnecessary when a with statement is used.
Could I see the definition of self.entryVariable?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.