[Tutor] Python ConfigParser Tkinter update configuration file between modules

Alan Gauld alan.gauld at yahoo.co.uk
Thu Apr 9 04:45:43 EDT 2020


The tutor list requires plain text to retain code formatting.
As you will see below your code is so screwed up its impossible to guess
what it loked ike originally.

Please repost using plain text.


On 09/04/2020 05:19, Niro One via Tutor wrote:
> I have two Tkinter windows main and configuration. Main window provides button link to configuration. In configuration main window editable. Configuration window provides button link for main window return. Configuration edit successful at Sypder IDE print. Configuration edit successful at write to disk. Returning to main window, configuration update FAIL. 
> 
> Close/restart main update PASS. Close/restart configuration update PASS.
> Changing 'r' to 'r+' at read_file wipes out configuration write.
> Thanks for having a look!
> 
> '''mdf.py'''
> import os
> import sysimport cnfimport tkinter as tk
> from configparser import ConfigParser
> config = ConfigParser(allow_no_value=True)
> cfg_key = 0
> if os.path.isfile('ini.ini') and os.access('ini.ini', os.R_OK):    try:        with open ('ini.ini', 'r') as cfg:            config.read_file(cfg)            print (config.sections())                        """[ins_exe]"""            ins_exe = config.get('ins_exe', 'ins_exe')
>             cfg.close()      except:        cfg_key = 1        print ("INI_ERR")        print ()
> else:    cfg_key = 1    print ("INI_BAK")    print ()
> if cfg_key == 1:    print ("INI_SET")    print ()
>     """[ins_exe]"""    ins_exe = '0'
> class Application(tk.Frame):    def __init__(self, *args, **kwargs):        super().__init__(*args, **kwargs)        self.GetChk()
>     def GetChk(self):        lbl_mdf = tk.StringVar()        lbl_mdf = tk.Label(self.master, text='mdf_exe')        lbl_mdf.place(x=35, y=15)        global ent_mdf        ent_mdf = tk.StringVar()        ent_mdf = tk.Entry(self.master, width=10, justify=tk.CENTER)        ent_mdf.place(x=25, y=40)        ent_mdf.insert(0, (ins_exe))        rtn_cnf = tk.Button(self.master, text='cnf_fmt', width=10,                            command=self.CnfOpn)        rtn_cnf.place(x=115, y=20)        run = tk.Button(self.master, text='mdf_exe', width=10,                        command=self.ChkOut)        run.place(x=115, y=55)        def ChkOut(self):        print ('mdf_exe =', ent_mdf.get())        self.FileOpn()        def CnfOpn(self, *args):        print ('mdf_cfg')        ask_cfg = tk.messagebox.askokcancel('mdf', " proceed cnf_fmt ? ")        if ask_cfg == True:            self.master.destroy()            cnf.CreateConfiguration()        else:            pass        def FileOpn(self):        stdoutOrigin=sys.stdout         sys.stdout = open('MAIN.txt', 'w')        print ("%")        print ()        print ('mdf_exe =', ent_mdf.get())        print ()        print ("%")        sys.stdout.close()        sys.stdout=stdoutOrigin
> def CreateApplication():    root = tk.Tk()    root.title('mdf')    root.geometry('210x100+125+250')    root.resizable(0,0)    app = Application(root)    app.mainloop()    if __name__ == "__main__":    CreateApplication()
> 
> '''cnf.py'''
> import osimport mdfimport tkinter as tk
> from configparser import ConfigParser
> config = ConfigParser(allow_no_value=True)
> cfg_key = 0
> if os.path.isfile('ini.ini') and os.access('ini.ini', os.R_OK):    try:        with open ('ini.ini', 'r') as cfg:            config.read_file(cfg)            print (config.sections())                        """[ins_exe]"""            ins_exe = config.get('ins_exe', 'ins_exe')
>             cfg.close()        except:        cfg_key = 1        print ("INI_ERR")        print ()
> else:    cfg_key = 1    print ("INI_BAK")    print ()
> if cfg_key == 1:        print ("INI_SET")    print ()
>     """[ins_exe]"""    ins_exe = '0'
> class Configuration(tk.Frame):    def __init__(self, *args, **kwargs):        super().__init__(*args, **kwargs)        self.GetChk()
>     def GetChk(self):
>         lbl_cnf = tk.StringVar()        lbl_cnf = tk.Label(self.master, text='cnf_fmt')        lbl_cnf.place(x=35, y=15)        global ent_cnf        ent_cnf = tk.StringVar()        ent_cnf = tk.Entry(self.master, width=10, justify=tk.CENTER)        ent_cnf.place(x=25, y=40)        ent_cnf.insert(0, (ins_exe))        rtn_mdf = tk.Button(self.master, text='mdf_exe', width=10,                            command=self.MdfOpn)        rtn_mdf.place(x=115, y=20)        run = tk.Button(self.master, text='cnf_fmt', width=10,                        command=self.ChkOut)        run.place(x=115, y=55)        def ChkOut(self):        print ('cnf_fmt =', ent_cnf.get())        self.FileOpn()
>     def MdfOpn(self, *args):        print ('mdf_cfg')        ask_cfg = tk.messagebox.askokcancel('cnf', " proceed mdf_exe ? ")        if ask_cfg == True:            self.master.destroy()            mdf.CreateApplication()        else:            pass
>     def FileOpn(self):                ins_exe =  ent_cnf.get()                config['ins_exe'] = {                'ins_exe' : ins_exe,                }                        with open('ini.ini', 'w+') as cfg:            print ('cnf_fmt = ini_w+')            config.write(cfg)            cfg.close()
> def CreateConfiguration():    root = tk.Tk()    root.title('cnf')    root.geometry('210x100+300+300')    root.resizable(0,0)    app = Configuration(root)    app.mainloop()    if __name__ == "__main__":    CreateConfiguration()
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
> 


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list