[Tutor] Python ConfigParser Tkinter update configuration file between modules

Alan Gauld alan.gauld at yahoo.co.uk
Fri Apr 10 06:34:34 EDT 2020


On 09/04/2020 14:31, Niro One via Tutor wrote:
I've pasted you code below. The two classes are so close
to identical that you should consider creating a superclass that they
can both inherit from. Then you just need to write the changes in each
and the common code will be shared. That aside, there are several common
issues in the code (I ignored the config stuff at the top, it looks ok
at a quick glance):

import mdf
ins_exe = config.get('ins_exe', 'ins_exe')

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)

Two things here.
First if you want to access lbl_cnf outside this method you must declare
it self.lbl_cnf otherwise the name dissappears when you exit the method.
It is a local variable.
Second, you create a StringVar then immediately throw it away by
reassigning the variable to a Label. That makes no sense.
I'm not sure what you thought would happen, but the first
assignment is basically doing nothing.


        global ent_cnf

This says look for a variable called ent_conf outside the class
(and if it doesn't exist creates one) That's really bad practice in a
class. Just create a class attribute called ent_cfg by adding a self prefix.

        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))

Same problem as before the StringVar is thrown away when you
reassign the variable. Although you don't appear to be using the
StringVars for anything so it doesn't matter apart from wasting
a few machine cycles...

        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

You seem to be closing the Tkinter GUI down then starting a new one, and
then repeating to come back. Are you aware of the TopLevel widget that
allows you to have multiple windows alive at the same time. You can then
just hide/show them to control visibility. Its much more efficient (and
faster0 that closing down the entire GUI framework and starting a new
event loop each time.

    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()


-- 
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