From: Florent R. <f.r...@fr...> - 2016-01-27 11:03:11
|
Hi Paraic, Paraic OCeallaigh <par...@gm...> wrote: > I am trying to use Editbox in a menu system to edit a configuration file. > I have successfully loaded the file into the editbox and can navigate and > edit text fine, but then when I press OK, it doesn't save the file. > I'm probably missing some basic understanding of how this works - do I need > to save the text that returns from the function in some way? Absolutely. The 'filepath' argument of Dialog.editbox() is only used to specify the *initial* contents of the dialog box (this is written in the doctring and pythondialog Manual). After this initialization of the widget, the editing process is only done in memory, thus it is up to you to save the returned string to a file or whatever storage you want, if you want the result to be saved at all (you could send it to the network, or wherever you want, really). Doing otherwise would restrict the generality of the widget a lot. For instance, when you get the resulting string, you may want to validate its syntax before maybe storing it to a file, and notify the user if the syntax is invalid, probably starting a new editbox with the edited contents loaded from a temporary file to allow the user to fix his errors, etc. (in a loop, until syntax is fine or the user gives up). I agree the initial file contents could be taken from a string instead of a file. Dialog.editbox() takes it from a file because it is a direct wrapper around dialog's --editbox option, which works this way. If it's really useful to have a function taking a string instead of a file path to specify the initial box contents, that can be done very easily in a wrapper function or method. Something like this should do the trick (untested): import dialog import tempfile class MyDialog(dialog.Dialog): def myEditBoxStr(self, initial_contents, *args, **kwargs): tmpfile = tempfile.NamedTemporaryFile( mode="w", prefix="yourProgramName.tmp", delete=False) try: with tmpfile as f: f.write(initial_contents) # The temporary file is now closed. According to the tempfile # module documentation, this is necessary if we want to be able # to reopen it reliably regardless of the platform. res = self.editbox(tmpfile.name, *args, **kwargs) finally: # The test should always succeed, but I prefer being on the # safe side. if os.path.exists(tmpfile.name): os.unlink(tmpfile.name) return res If this is really useful, I could add some similar wrapper to pythondialog as a Dialog.editboxStr() method, or something like that. > I had a look at the dem.py but I don't see the editbox in the demo saving > anything after it loads the file (not surprising since its the /etc/passwd > it loads!) That is correct! Regards -- Florent |