-1

单击主窗口中的按钮后,我想在其他类中运行函数。我应该如何将值 [id_entry] 从类 [StartingPage] 中的数据输入传递到第二类 [Graph] 中的函数 [read_data]。单击“分析费用”按钮后,我想传递值并运行功能。可能吗?

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)
4

1 回答 1

0

您应该托管id_entryMainWindow因为您已经将其传递parent给其他两个类:

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

还要注意我是如何交换轮次的parentwindow所以它parent指的是父类并window指的tk.Frame()是在MainWindow.

于 2020-11-08T11:46:16.040 回答