I have a .ini file which holds the default values for the Checkbuttons/Radiobuttons/Entry components of my app.
I want to use a 'SAVE' button in another frame to save the updated values by user and update the ini file.
Initially I tried to bypass the problem by having all the values in a dictionary, then make each Checkbuttons to call a method to update the dictionary, but then I realized I cannot call a command inside ttk.Entry()
...then how should I retrieve its value in my 'SAVE' button frame?
My code so far:
import tkinter as tk
from tkinter import ttk
import configparser
class iniParser(configparser.ConfigParser):
"""Return dictionary with the config information"""
def as_dict(self):
d = dict(self._sections)
for k in d:
d[k] = dict(self._defaults, **d[k])
d[k].pop('__name__', None)
return d
global d
parser = iniParser()
parser.read('Config.ini')
d = parser.as_dict()['CONFIG']
class CheckButtonFrame(ttk.Frame):
def __init__(self, parent):
super().__init__(parent)
self.rowconfigure(0, weight=1)
self.rowconfigure(1, weight=1)
self.__create_widgets()
def __create_widgets(self):
# Create control variables
self.check1 = tk.BooleanVar(value=d['check1'])
self.check2 = tk.BooleanVar(value=d['check2'])
check_frame = ttk.LabelFrame(self, text="Checks", padding=(20, 10))
check_frame.grid(row=0, column=0, padx=(20, 10), pady=(5, 5), sticky="nsew")
# Checkbuttons
check_1 = ttk.Checkbutton(check_frame, text="Checkbox1", variable=self.check1, command=lambda:self.update_val('check1',self_check1))
check_1.grid(row=0, column=0, padx=3, pady=5, sticky=tk.NSEW)
check_2 = ttk.Checkbutton(check_frame, text="Checkbox2", variable=self.check2, command=lambda:self.update_val('check2',self_check2))
check_2.grid(row=1, column=0, padx=3, pady=5, sticky=tk.NSEW)
def update_val(self, text, variable):
d[text] = variable.get()
class EntryFrame(ttk.Frame):
def __init__(self,parent):
super.__init__(parent)
self.rowconfigure(0,weight=1)
self.rowconfigure(1,weight=1)
self.__create_widgets()
def __create_widgets(self):
self.rowconfigure(0,weight=1)
self.rowconfigure(1,weight=1)
self.path = tk.StringVar(value=d['path'])
entry_frame = ttk.LabelFrame(self, text="Path", padding=(20, 10))
entry_frame.grid(row=0, column=0, padx=(20, 10), pady=(10, 10), sticky="nsew")
ttk.Label(entry_frame, text='Path:').grid(row=0, column=0, sticky=tk.W, pady=10)
keyword = ttk.Entry(small_frame, width=30, textvariable=self.path)
keyword.focus()
keyword.grid(row=1, column=0, columnspan=2, sticky=tk.W)
Code for my ButtonFrame is left out. It just has a button now which is ready to call a function when clicked, but the problem is I'm not sure how to get the keyword.get() from EntryFrame to my ButtonFrame
The global dictionary method only worked for the Checkbuttons
Can someone please help me? Thank you!