0

I am working on creating a basic GUI in python using tkinter. I want the get_analysis() function to be called on the switching of the page.(Switching from one page/window to another) However, I am yet to find how to call this function from the button in MainView that will take me to the AnalysisPage. I get the following error.

Error: Analysis Page doesn't have a function called get_analysis().

class AnalysisPage(Page):
    def __init__(self, *args, **kwargs):
        Page.__init__(self, *args, **kwargs)
        label = tk.Label(self, text="Analysis Page", font=(NORMAL, 18))
        label.place(relwidth=0.4, relheight=0.05, relx=.01, rely=0)

        tree = ttk.Treeview(self)
        tree.place(relwidth=.65, relheight=.8, relx= 0.01, rely=.1)
        #Columns
        tree['columns'] = ('number', 'name', 'status', 'parameters', 'duration')
        tree.column("#0", width=0, stretch=NO)#"", width=, minwidth=)
        tree.column("number", anchor=CENTER, width=10)
        tree.column("name", anchor=W, width=100)
        tree.column("status", anchor=CENTER, width=20)
        tree.column("parameters", anchor=W, width=20)
        tree.column("duration", anchor=CENTER, width=20)

        #heading names
        tree.heading('number', text='#', anchor=CENTER) #add anchor E C W to do text alignment
        tree.heading('name', text='Test', anchor=CENTER)
        tree.heading('status', text='Status', anchor=CENTER)
        tree.heading('parameters', text='Parameters', anchor=W)
        tree.heading('duration', text='Duration', anchor=CENTER)

        #Scrollbar
        scrollbar = ttk.Scrollbar(self, orient=tk.VERTICAL, command=tree.yview)
        tree.configure(yscroll=scrollbar.set)
        scrollbar.place(relwidth=.01,relheight=.8,relx=.65,rely=.1)
        #Tree definition
        def item_selected(a):
            curItem=tree.focus()
            #print(tree.item(curItem))

            clear_test()
            test_analysis(tree.item(curItem)['values'][1], tree.item(curItem)['values'][0])
        tree.bind('<<TreeviewSelect>>', item_selected)

        Summary = tk.Text(self, wrap="none"
        )
        Summary.place(relwidth=.28, relheight=.8,relx=.7, rely=.1)

        def get_analysis():
            #Combine the buttons now and then link them so that it will act as a refresh every time.

            # Check for regression.log or regression_status.log
            # Finished will have regression.log and all information will be there
            # Regression_status.log will have a general log for current tests that have run
            global WorkingDir
            #print(WorkingDir)
            data_analysis = ["","","","",""]
            file = FILE_PATH + WorkingDir + "\\regression_status.txt"
            index = 0
            passed = 0
            #print(file)
            if exists(file):
                with open(file, "r") as file1:
                        for line in file1:
                            if index != 0:
                                for char in line:
                                    if (char != ' ' and passed == 0):
                                        data_analysis[passed] += char
                                    elif(char != ' ' and passed == 1):
                                        data_analysis[passed] += char
                                    elif(char != ' ' and passed == 2):
                                        data_analysis[passed] += char
                                    elif(char != ' ' and passed == 3):
                                        data_analysis[passed] += char
                                    elif(data_analysis[passed] != ""):
                                        passed+=1
                                #print(data_analysis[0], data_analysis[1], data_analysis[2], data_analysis[3])
                                tree.insert('', index, values=(data_analysis[0], data_analysis[1], data_analysis[2], data_analysis[3]))
                                data_analysis = ["","","","",""]
                                passed = 0
                            index+=1

        def test_analysis(select, test):
            select = FILE_PATH + WorkingDir + "\\" + select + "_" + str(test) + "\\qsub.out"
            with open(select, "r") as file2:
                for line in file2:
                    Summary.insert(tk.END, line)

        # def treeview_sort_column(treeview: ttk.Treeview, col, reverse: bool):
        #     try:
        #         data_list = [
        #             (int(treeview.set(k, col)), k) for k in treeview.get_children("")
        #         ]
        #     except Exception:
        #         data_list = [(treeview.set(k, col), k) for k in treeview.get_children("")]

        #     data_list.sort(reverse=reverse)

        #     # rearrange items in sorted positions
        #     for index, (val, k) in enumerate(data_list):
        #         treeview.move(k, "", index)

        #     # reverse sort next time
        #     treeview.heading(
        #         column=col,
        #         text=col,
        #         command=lambda _col=col: treeview_sort_column(
        #             treeview, _col, not reverse
        #         ),
        #     )
        # columns = ('number', 'name', 'status', 'duration')
        # for col in columns:
        #     tree.heading(col, text=col, command=lambda _col=col: treeview_sort_column(tree, _col, False))

        def clear_test():
            Summary.delete("1.0", "end")

        def clearData():
            tree.delete(*tree.get_children())

        def buttonCall():
            clearData()
            get_analysis()

        #"C:\Users\shado\OneDrive\Documents\Dad_Project\regressions\rtl.brentr.2022_01_17_11_30_18\regression.log"
        #get_analysis(filename)
        RefreshButton = tk.Button(self, text="Refresh", command=buttonCall)
        RefreshButton.place(relwidth=.1, relheight=.1, relx=.7, rely=.0)        
        
 
class MainView(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        p1 = RegressionPage(self)
        p2 = AnalysisPage(self)
        
        buttonframe = tk.Frame(self)
        container = tk.Frame(self, bg="green")
        buttonframe.pack(side="top", fill="x", expand=False)
        container.pack(side="top", fill="both", expand=True)

        p1.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
        p2.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
        #p3.place(in_=container, x=0, y=0, relwidth=1, relheight=1)

        RegressionButton = tk.Button(buttonframe, height=2, width=15, text="Regression", command=p1.show)
        AnalysisButton = tk.Button(buttonframe, height=2, width=15, text="Analysis", command=lambda:[p2.RefreshButton,p2.show])
        #b3 = tk.Button(buttonframe, text="Page 3", command=p3.show)

        RegressionButton.pack(side="left", expand=True)
        AnalysisButton.pack(side="right", expand=True)
        #b3.pack(side="left")

        p1.show()
Faraaz Kurawle
  • 1,085
  • 6
  • 24

1 Answers1

0

You have used nested function or inner function, here it is get_analysis()

Nested function: A function inside a function

So, if you try to access nested function from another function(not its parent function) you will get the error.

Hence your solution is that don't use nested function if you know you are going to use that particular function from another class or function.

Only use nested function if you are 100% sure that that function will not be used by any other class or function other than the parent function.

def This_is_a_parent_func(): # PARENT FUNCTION
    def This_is_a_nested_func_or_a_child_func_of_the_above_func(): # CHILD OR NESTED FUNCTION
        # This function can only be accessed by This_is_a_parent_func(), not by other function outside the main or parent function, here not out side the This_is_a_parent_func().
        pass

Important Refrences:

For calling a class from another class, refer this question

Faraaz Kurawle
  • 1,085
  • 6
  • 24