Coder Profile - Show off your skills, get a coder profile.
 
 
 
  
Posted: 3.79 Years Ago 

mikeMarek
Canada
Contrib Level: 5
Total Posts: 151
I've recently delved into Tkinter, and am trying to develop a simple notepad. The only problem s that it has problem saving files. Here's my code for saving:
CODE: Copy / Restore  ::  Remove Scroll Bars
  1. def filesave():
  2.        # save file
  3.        try:
  4.               openfile = open("external file.txt", "w")
  5.               openfile.write(getAllText())
  6.               openfile.close()
  7.        except:
  8.               showwarning("Error saving file",
  9.                                    "Cannot save file (%s)" % filename)
  10.        # open file and save text field contents
And here's the code to select what you want to save the file as:
CODE: Copy / Restore  ::  Remove Scroll Bars
  1. def filesaveas():
  2.        # save file as...
  3.        try:
  4.               asksaveasfilename(filetypes=formats, title="Save As...")
  5.        except:
  6.               showwarning("Error saving file",
  7.                                    "Cannot save file (%s)" % filename)
  8.        # save file as specified type
But both return an error message and don't save. Any idea why this might be?

Thanks for any help,
-Mike
Posted: 3.79 Years Ago 

gunni
Germany
Contrib Level: 7
Total Posts: 412
Doesn't tkinter's (never even heard of it) exceptions carry information? in PHP you can throw exception objects that carry the file and line number where it occured as well as a programmer defined text and an error code. In C++ you have to throw any kind of value (e.g. an integer). Isn't there something alike in tkinter?
Posted: 3.79 Years Ago 

mikeMarek
Canada
Contrib Level: 5
Total Posts: 151
I don't think so, but I did manage to fix it. However, I have run into another problem: Whenever I click "Save", no matter what, it takes me to the "Save As..." function; even if the file is already set. Here's my 2 new functions:
CODE: Copy / Restore  ::  Remove Scroll Bars
  1. filename = "Untitled"
  2. # this is the filename that we start with
  3.  
  4. def filesave():
  5.        # save file
  6.        if filename == "Untitled":
  7.               filesaveas()
  8.        else:
  9.               openfile = open(filename, "w")
  10.               openfile.write(app.text.get(1.0, END))
  11.               openfile.close()
  12.               setTitle(filename)
  13.        # open file and save text field contents
  14.  
  15. def filesaveas():
  16.        # save file as...
  17.        filename = asksaveasfilename(filetypes=formats, title="Save As...")
  18.        if not (filename == ""):
  19.               openfile = open(filename, "w")
  20.               openfile.write(app.text.get(1.0, END))
  21.               openfile.close()
  22.               setTitle(filename)
  23.        # save file as specified type
If anyone wants to see the whole source code just ask. Any help would be greatly appreciated!
-Mike
Posted: 3.79 Years Ago 

gunni
Germany
Contrib Level: 7
Total Posts: 412
Again I can only make guesses. This time: scope/variable visibility problems.
Posted: 3.79 Years Ago 

mikeMarek
Canada
Contrib Level: 5
Total Posts: 151
Post Quote - Direct Reference
That seems to be the problem. I checked and traced "filename" whenever the user (me :P) saves, and "filename" doesn't change. It stays as "Untitled".

Cheers,
-Mike
Page 1 of 1
 
 
Latest News About Coder Profile
Coder Profile Poll
Why do you get bored with programming?

Not enough time to do something productive
I run out of ideas
Too hard to show people my creations
Everything i do has too many errors, and it's too hard
I don't get bored!!!


please login to cast your vote
and see the results of this poll
Latest Coder Profile Changes
Coder Profile was last updated
3.49 Years Ago
Official Blog :: Make A Donation :: Credits :: Contact Me
Terms & Conditions :: Privacy Policy :: Documents :: Wallpapers
Version 1.46.00
Copyright © 2007 - 2012, Scott Thompson, All Rights Reserved