rmcknst2
rmcknst2

Reputation: 307

Committing multiple config file changes on button press with tkinter and configparser

I have a program that, has a menu built with tkinter. There are several buttons on the menu and when pressed allows the user to pick certain locations for files. Here is that code:

def open_vend_direct():
    vend_directory = filedialog.askopenfilename(
        initialdir="/", title="Select file", filetypes=(("Excel Files (CSV)", "*.csv"), ("all files", "*.*")))
    parser = ConfigParser()
    parser.read('config.ini')
    parser.set('VendorList','List_Location',vend_directory)

def open_attach_direct():
    vend_attach_direct = filedialog.askdirectory()
    parser = ConfigParser()
    parser.read('config.ini')
    parser.set('VendorFile','file_Location',vend_attach_direct)

def open_log_direct():
    log_locate = filedialog.askdirectory()
    parser = ConfigParser()
    parser.read('config.ini')
    parser.set('LogFolder','log_location',log_locate)

I have another button that is supposed to Apply all changes. For this function I tried this but it doesn't work:

def apply_option():
    parser = ConfigParser()
    parser.read('config.ini')
    with open('config.ini', 'w') as f:
        parser.write(f)

In the three button functions I used to have this:

with open('config.ini', 'w') as f:
        parser.write(f)

This worked but the problem is everytime the user changes that file location it would save and update the program automatically. I would only like the changes to be saved when the "Apply Changes" button is pressed.

Edit: I also have other options on the menu (checkbuttons that I would like the apply changes to affect as well)

Is it because they are all in different functions?

Upvotes: 0

Views: 64

Answers (1)

Robot Mind
Robot Mind

Reputation: 321

Every time you initiate parser and read file you reset your parser to values in yours config.ini. So put initiation of parser and parser read out of functions in global. Then it should work.

Upvotes: 1

Related Questions