0

我正在使用 tkinter 在 python 中创建一个基本的 GUI。我希望在get_analysis()切换页面时调用该函数。(从一个页面/窗口切换到另一个)但是,我还没有找到如何从按钮调用这个函数MainView,它将带我到AnalysisPage. 我收到以下错误。

错误:分析页面没有名为 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()
4

1 回答 1

0

你使用了嵌套函数或内部函数,这里是get_analysis()

嵌套函数:函数内部的函数

因此,如果您尝试从另一个函数(而不是其父函数)访问嵌套函数,您将收到错误消息。

因此,如果您知道要使用另一个类或函数中的特定函数,则您的解决方案是不要使用嵌套函数。

仅当您 100% 确定该函数不会被父函数以外的任何其他类或函数使用时,才使用嵌套函数。

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

重要参考:

要从另一个类调用一个类,请参考这个问题

于 2022-02-28T19:50:52.243 回答