How to get value from a text field in another module? How to get value from a text field in another module? tkinter tkinter

How to get value from a text field in another module?


Alright, let's start from the beginning, here's a working example of your code:

import tkinterclass guitest:    def __init__(self):        win1 = tkinter.Tk()        self.field1 = tkinter.Text(win1)        self.field1.grid(column=0, row=0)        self.but1 = tkinter.Button(win1, text='click', command=self.getVar)        self.but1.grid(column=1, row=0)        win1.mainloop()    def getVar(self):        captured = str(self.field1.get("1.0", tkinter.END))        print capturedinst1 = guitest()

Now, before breaking down that piece of code, you should ask yourself if the reason you want to is strong enough. In case your answer is affirmative (think it twice) one possible way to do it would be this:


# main.pyimport guiinst1 = gui.guitest()

# gui.pyimport tkinterimport defsclass guitest:    def __init__(self):        win1 = tkinter.Tk()        self.field1 = tkinter.Text(win1)        self.field1.grid(column=0, row=0)        self.but1 = tkinter.Button(win1, text='click', command=self.getVar)        self.but1.grid(column=1, row=0)        win1.mainloop()    def getVar(self):        defs.getVar(self)

# defs.pyimport tkinterdef getVar(guitest_inst):    captured = str(guitest_inst.field1.get("1.0", tkinter.END))    print captured

But again, think twice before breaking down widgets like this... just saying :)