samedi 13 juin 2020

How to save selected checkbox values in a text file using tkinter

I want to save or write in a text file all the selected checkbox. but it doesn't give me any value in the text file. Can I use if and elif statement on this for me to save the selected check box value in a text file?

var1 = StringVar()
chk1 = Checkbutton(frame6, text="Sports", variable=var1,onvalue="Sports", offvalue="").grid(row=2, 
sticky=N)
var2 = StringVar()
chk2 = Checkbutton(frame6, text="Music", variable=var2,onvalue="Music", offvalue="").grid(row=3, sticky=N)
var3 = StringVar()
chk3 = Checkbutton(frame6, text="Arts", variable=var3,onvalue="Arts", offvalue="").grid(row=4, sticky=N)
var4 = StringVar()
chk4 = Checkbutton(frame6, text="Science", variable=var4,onvalue="Science", offvalue="").grid(row=5, sticky=N)
var5 = StringVar()
chk5 = Checkbutton(frame6, text="Technology", variable=var5,onvalue="Technology", offvalue="").grid(row=6, sticky=N)
var6 = StringVar()
chk6 = Checkbutton(frame6, text="Nature", variable=var6,onvalue="Nature", offvalue="").grid(row=7, sticky=N)

btnSave= Button(frame8, text= "Save", fg="White", bg="Gray",font = "-family {Segoe UI} -size 12 -weight bold ", command = save )
btnSave.grid(row = 0, column = 1)
def var_states():
   res = (var1.get(), var2.get(),var3.get(),var4.get(),var5.get(),var6.get())
   res.append(Lists)
   print(res)
   return res
def save():
     with open('StudentInfo', 'w') as f:
          f.write(f"\n{var_states.get()}")
          f.close()

here is my output on this code. can I use strip() here to remove other characters?

123,qweqwe,qweqw,qweqwe,Female,('', 'Music', 'Arts', '', '', '')
# I want to remove the other characters like open paranthesis and other comma



Aucun commentaire:

Enregistrer un commentaire