Python 如何将输入值(TKinter)传递给其他类中的函数,并在单击按钮后使用传递的值运行函数

Python 如何将输入值(TKinter)传递给其他类中的函数,并在单击按钮后使用传递的值运行函数,python,tkinter,python-3.8,python-class,Python,Tkinter,Python 3.8,Python Class,我想在单击主窗口中的按钮后在其他类中运行函数。我应该如何将值[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)

我想在单击主窗口中的按钮后在其他类中运行函数。我应该如何将值[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)

您应该在
main窗口中托管
id\u条目
,因为您已经将其作为
parent
传递到其他两个类中:

class主窗口(tk.tk):
定义初始化(self,*args,**kwargs):
tk.tk.\uuuuu初始化(self,*args,**kwargs)
tk.tk.iconbitmap(self)
tk.tk.wm_标题(自“数据分析器”)
窗口=传统框架(自)
window.pack(side='top',fill='both',expand=True)
window.grid_rowconfigure(0,权重=1)
window.grid\u column配置(0,权重=1)
self.frames={}
对于F英寸(起始页,图表):
帧=F(自,窗口)
self.frames[F]=帧
frame.grid(行=0,列=0,粘性='nsew')
self.show\u gui(起始页)
def显示图形用户界面(自身,窗口):
frame=self.frames[窗口]
frame.tkraise()
类起始页(tk.Frame):
定义初始化(自、父、窗口):
tk.Frame.\uuuu init\uuuuu(自,父)
label=tk.label(self,text='Upload document')
标签包装(pady=10,padx=10)
button1=ttk.Button(self,text='Upload XLS file',
command=lambda:self.open_file())
按钮1.pack()
button2=ttk.按钮(self,text='analysis expense',
command=lambda:parent.show\u gui(图形))
按钮2.pack()
self.parent.id_entry=ttk.entry(self)
self.parent.id_entry.pack()
def get_字符串(自身):
返回self.parent.id\u entry.get()
def open_文件(自身):
file=askopenfile(mode='r',filetypes=[('excel files','*.xlsx'))
export\u do\u SQL.export\u to\u SQL(file.name)
类图(tk.Frame):
定义初始化(自、父、窗口):
tk.Frame.\uuuu init\uuuuu(自,父)
label=tk.label(self,text='Expense analyzer')
标签包装(pady=10,padx=10)
button1=ttk.Button(self,text='Return',
command=lambda:parent.show\u gui(起始页))
按钮1.pack()
id\u元素=读取数据(来自输入的数据)
def读取_数据(自身):
#在此处使用self.parent.id\u条目

还要注意我是如何交换
parent
window
的,以便
parent
引用父类,
window
引用在
main window
中创建的
tk.Frame()
当我运行它时,有:
self.parent.id\u entry=ttk.entry(self)AttributeError:“StartingPage”对象没有属性“parent”
Ok,可能您的两个类也需要self.parent=parent