0

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!

wk14
  • 197
  • 1
  • 7

1 Answers1

0

I was able to solve my issue by spending some time to understand the concept in How to access variables from different classes in tkinter?
Controller is essentially the main app which contains all other frames/components,so I just needed to declare my tk variables in there and pass to each sub-frames.

class MainApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.rowconfigure(0, weight=1)
        self.rowconfigure(1, weight=1)
        self.rowconfigure(2, weight=1)
        self.rowconfigure(3, weight=1)
        self.columnconfigure(0, weight=1)
        self.columnconfigure(1, weight=1)
        self.columnconfigure(2, weight=1)
        self.config_data = {
            "check1": tk.BooleanVar(value=d['check1']),
            "check2": tk.BooleanVar(value=d['check2'])
        }
        self.__create_widgets(self.config_data)
    def __create_widgets(self):
        first_frame = FirstFrame(self)
        button_frame = ButtonFrame(self)
    ...
wk14
  • 197
  • 1
  • 7