使用tkinter和configparser在按钮按下时提交多个配置文件更改

时间:2019-06-24 19:48:31

标签: python parsing tkinter

我有一个程序,有一个用tkinter构建的菜单。菜单上有几个按钮,按下后允许用户选择文件的特定位置。这是代码:

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)

我还有另一个应该应用所有更改的按钮。对于此功能,我尝试了此操作,但不起作用:

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

在我曾经使用的三个按钮功能中:

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

这有效,但是问题是每次用户更改文件位置时,它将自动保存并更新程序。我只希望在按下“应用更改”按钮时保存更改。

编辑:菜单上还有其他选项(我也希望应用更改也能生效的复选框)

是因为它们都具有不同的功能吗?

1 个答案:

答案 0 :(得分:1)

每次启动解析器并读取文件时,都会将解析器重置为config.ini中的值。 因此把解析器的初始化和解析器从全局函数中读出来。 然后它应该工作。