Dogeek
Dogeek

Reputation: 302

asksaveasfile dialog in python

Okay, so i am developping a DnD app, and i want to save character sheets using the filedialog.asksaveasfile() command.

Here's the code:

def save(self):
    file_=filedialog.asksaveasfile(parent=self, mode="w",title=\
"Save your Character Sheet",defaultextension=".dndcs",filetypes=[("Character Sheets", ".dndcs"), ("All Files", ".*")])
    #file_= open(str(saveName),"w")
    string=""
    list_vars=[self.player,self.character, self.dm,self.str,self.int,self.wis,self.dex,self.con,self.cha,self.JPpoison,\
               self.JPwands,self.JPparalysis,self.JPbreath,self.JPspell,self.class_,self.race,self.align,self.AC,self.HP,self.maxHP,self.XPtotal]
    for i in range(len(list_vars)):
        string=str(list_vars[i].get())+"\n"
        print(string)
    file_.write(string)
    file_.close()

It doesn't return any kind of exception, but when i try to open the file, using an editor, all i see is a 0 on the first line.

What goes wrong ?

Upvotes: 0

Views: 545

Answers (1)

Dogeek
Dogeek

Reputation: 302

So after numerous tries to fix that, i noticed i made a small mistake. Indeed, it's not :ù

string=str(list_vars[i].get())+"\n"

But :

string+=str(list_vars[i].get())+"\n"

Apart from that, this code works, feel free to use it at will.

Upvotes: 3

Related Questions