0

I made a gui which collects data, the data is saved in sqlite3 and shown in a listbox.

Made a function which sends the user a mail with the data of a selected row, and give that row the color yellow.

The problem is that the program does not save these color changes. When i quit and run the program again, the color changes back to white again.

Made a simple example which shows the problem:

from tkinter import *

top = Tk()
top.geometry("800x400+0+0")

Lb1 = Listbox(top)
Lb1.insert(1, "Python")
Lb1.insert(2, "Perl")
Lb1.insert(3, "C")
Lb1.insert(4, "PHP")
Lb1.insert(5, "JSP")
Lb1.insert(6, "Ruby")
Lb1.pack()

def color_yellow():
    Lb1.itemconfig(Lb1.curselection()[0], {'bg':'Yellow'})

b1=Button(top,text="color row Yellow",width=16,command=color_yellow)
b1.place(relx=0.5, rely=0.6, anchor=CENTER)


top.mainloop()

Tried with pickle but can't get it working:

from tkinter import *
import pickle

top = Tk()
top.geometry("800x400+0+0")

Lb1 = Listbox(top)
Lb1.insert(1, "Python")
Lb1.insert(2, "Perl")
Lb1.insert(3, "C")
Lb1.insert(4, "PHP")
Lb1.insert(5, "JSP")
Lb1.insert(6, "Ruby")
Lb1.pack()

def color_yellow():
        Lb1.itemconfig(Lb1.curselection()[0], {'bg':'Yellow'})
        return

pickle_out = open("dict.pickle","wb")
pickle.dump(color_yellow, pickle_out)
pickle_out.close()

pickle_in = open("dict.pickle","rb")
color_yellow = pickle.load(pickle_in)

b1=Button(top,text="color row Yellow",width=16,command=color_yellow)
b1.place(relx=0.5, rely=0.6, anchor=CENTER)


top.mainloop()
Rubinjo13
  • 65
  • 2
  • 11
  • Well, i.m.o. it is pretty unclear what you are trying to achieve.. But, in general, if you'd like to save some data from within a loop I'd recommend either having the function return the wanted data or append it to a list that's external but accessible from within the function (either as a function argument or (if absolutely necessary) as a global list). – Montmons Mar 24 '17 at 13:54
  • Thanks for your response. i added some for information. Hope it is a bit clearer now. – Rubinjo13 Mar 24 '17 at 14:22
  • Edited my answer as per the new input you provided – Montmons Mar 24 '17 at 18:11

1 Answers1

0

EDIT

Ok, so given your latest adaptions the question has become a bit more complete. However, it still isn't a minimum, complete, and verifiable example. This makes it hard to pin down what causes your problem.

As becomes clear via another attempt I've made to recreate your problem. However, in my example the list items keep their color once the button is pressed. Meaning, I am able to color all list items yellow. Why this does not happen for you could have multiple causes.. as is now hard to tell. (I am wondering for example: did you maybe set an explicit select mode on your Listbox? e.g. SINGLE, BROWSE, MULTIPLE or EXTENDED)

from tkinter import * # Asterix import for test purposes, try to avoid copying this.

master = Tk()

listbox = Listbox(master)
listbox.pack()

listbox.insert(END, "a list entry")

for item in ["one", "two", "three", "four"]:
    listbox.insert(END, item)

def select(listbox):
    listbox.itemconfig(listbox.curselection()[0], bg='yellow')

button = Button(master, text='Select', command=lambda: select(listbox))
button.pack()

mainloop()
Montmons
  • 1,416
  • 13
  • 44
  • Thanks for pointing out to the minimum, complete, and verifiable example. Made it much clearer for myself to. Just made a clear example of the problem. Hope it is clear now:) – Rubinjo13 Mar 26 '17 at 14:56
  • Ok, so I will probably be able to help you out but short on time today. Quick guess: try to move your `Lb1.pack()` statement to the end of your insertions. So before defining and calling your function. See if that helps. Otherwise, leave a comment and I'll have another look tomorrow. – Montmons Mar 26 '17 at 15:20
  • No, when i restart the program the rows are white again. Maybe i can somehow copy the output of the function to another script, and than input that script? – Rubinjo13 Mar 26 '17 at 15:38
  • Ah wait.. now I see, you want to save the color after the app has been stopped.. hmm let me think about that for a while.. you could offcourse store the lists properties in an external file and read them from there but that would be ugly.. I have never developed programs that needed to save data over different runs but I believe there exists sophisticated ways of doing so.. there is a specific python library I believe.. let's see if I can find it.. – Montmons Mar 26 '17 at 15:39
  • Ah yes, see, the pickle module might be what you are looking for. [As described here](http://stackoverflow.com/q/31891286/4041795) – Montmons Mar 26 '17 at 15:45
  • tried the pickle method(see first post), but can't get it to work. Color is still changing to white. – Rubinjo13 Mar 26 '17 at 17:51
  • Ah.. just found [this](http://stackoverflow.com/a/40245302/4041795), apparently pickle and Tkinter do not play nice together.. – Montmons Mar 28 '17 at 12:12
  • Ah ok, to bad! Thanks for the information. Do you know an other solution? Or shall i post this as a new question? – Rubinjo13 Mar 28 '17 at 14:36