-1

I want to run function in other class, after click on button in main window. How should I pass the value[id_entry] from data input in Class[StartingPage] to function[read_data] in second class[Graph]. I want pass value and run function after click on "Analyse expense" button. Is it possible?

class MainWindow(tk.Tk):

def __init__(self, *args, **kwargs):
    tk.Tk.__init__(self, *args, **kwargs)
    tk.Tk.iconbitmap(self)
    tk.Tk.wm_title(self, 'Data analyser')

    window = tk.Frame(self)
    window.pack(side='top', fill='both', expand = True)
    window.grid_rowconfigure(0, weight=1)
    window.grid_columnconfigure(0, weight=1)

    self.frames = {}

    for F in (StartingPage, Graph):
        frame = F(window, self)
        self.frames[F] = frame
        frame.grid(row = 0, column = 0, sticky = 'nsew')

    self.show_gui(StartingPage)

def show_gui(self, window):
    frame = self.frames[window]
    frame.tkraise()


class StartingPage(tk.Frame):

def __init__(self, parent, window):
    tk.Frame.__init__(self, parent)
    label = tk.Label(self, text = 'Upload document')
    label.pack(pady = 10, padx = 10)

    button1 = ttk.Button(self, text = 'Upload XLS file',
                           command=lambda: self.open_file())
    button1.pack()

    button2 = ttk.Button(self, text = 'Analyse expense',
                           command=lambda: window.show_gui(Graph))
    button2.pack()
    id_entry = ttk.Entry(self)
    id_entry.pack()

def get_string(self):
    return self.id_entry.get()

def open_file(self):
    file = askopenfile(mode='r', filetypes=[('excel files', '*.xlsx')])
    export_do_SQL.export_to_sql(file.name)

class Graph(tk.Frame):

def __init__(self, parent, window):
    tk.Frame.__init__(self, parent)
    label = tk.Label(self, text = 'Expense analyser')
    label.pack(pady = 10, padx = 10)

    button1 = ttk.Button(self, text = 'Return',
                           command=lambda: window.show_gui(StartingPage))
    button1.pack()


    id_element = read_data(DATA FROM INPUT)
Bartol
  • 83
  • 1
  • 9

1 Answers1

0

You should host id_entry in MainWindow as you are already passing that as parent into the other two classes:

class MainWindow(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.iconbitmap(self)
        tk.Tk.wm_title(self, 'Data analyser')
    
        window = tk.Frame(self)
        window.pack(side='top', fill='both', expand = True)
        window.grid_rowconfigure(0, weight=1)
        window.grid_columnconfigure(0, weight=1)
    
        self.frames = {}
    
        for F in (StartingPage, Graph):
            frame = F(self, window)
            self.frames[F] = frame
            frame.grid(row = 0, column = 0, sticky = 'nsew')
    
        self.show_gui(StartingPage)
    
    def show_gui(self, window):
        frame = self.frames[window]
        frame.tkraise()


class StartingPage(tk.Frame):

    def __init__(self, parent, window):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text = 'Upload document')
        label.pack(pady = 10, padx = 10)
    
        button1 = ttk.Button(self, text = 'Upload XLS file',
                               command=lambda: self.open_file())
        button1.pack()
    
        button2 = ttk.Button(self, text = 'Analyse expense',
                               command=lambda: parent.show_gui(Graph))
        button2.pack()
        self.parent.id_entry = ttk.Entry(self)
        self.parent.id_entry.pack()
    
    def get_string(self):
        return self.parent.id_entry.get()
    
    def open_file(self):
        file = askopenfile(mode='r', filetypes=[('excel files', '*.xlsx')])
        export_do_SQL.export_to_sql(file.name)

class Graph(tk.Frame):

    def __init__(self, parent, window):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text = 'Expense analyser')
        label.pack(pady = 10, padx = 10)
    
        button1 = ttk.Button(self, text = 'Return',
                               command=lambda: parent.show_gui(StartingPage))
        button1.pack()
    
    
        id_element = read_data(DATA FROM INPUT)
    def read_data(self):
        # use self.parent.id_entry here

Also note how I have swapped round parent and window so that parent refers the parent class and window refers to the tk.Frame() created in MainWindow.

quamrana
  • 37,849
  • 12
  • 53
  • 71
  • When I'm runnig it, there is: `self.parent.id_entry = ttk.Entry(self) AttributeError: 'StartingPage' object has no attribute 'parent'` – Bartol Nov 08 '20 at 12:53
  • Ok, maybe your two classes also need self.parent = parent – quamrana Nov 08 '20 at 13:18