Python - GUI (TKinter), getting variables from other functions without running the whole function agan
<p>I do have an problem i can't realy get solved. The problem is, i do have two separate buttons. In one button I want to load a selected file. And with the other one i want to do some searching.
However i can't get de variables from one function to the otherone without running the whole function again. So that means, the find button is useless now.</p>

<pre><code>from __future__ import print_function
from Tkinter import *
from Tkinter import Tk
from tkFileDialog import askopenfilename


def openFile():
    Tk().withdraw()
    txtFile = askopenfilename(defaultextension=".txt", filetypes=(("something", "*.txt"),("All Files", "*.*") ))
    print(txtFile)
    return txtFile


def Function():
    txtFile = openFile()
    with open(txtFile) as fp, open(('c:/map/test.txt'), 'w') as fo:
        for line in fp:

            if ('Hello') in line:
                content = line.strip() + " Hello detected "
            else:
                content = line.strip()
            fo.write(content + "\n")


def presentGUI():
    root = Tk()
    root.title("simulation")

    # Buttons
    button1 = Button(root, text="Select .txt file", command=openFile)
    button2 = Button(root, text="Run !", width=28, command=Function)

    # grid
    button1.grid(row=1, column=1)
    button2.grid(row=3, columnspan=3)

    root.mainloop()

presentGUI()
</code></pre>

<p>Two methods</p>

<ol>
<li>Use a global variable for txtFile</li>
<li>Use OO and create the functions as part of a class so that they can share variables. </li>
</ol>

<p>I've given you an example of option 2. Since It is my preferred method of working.</p>

<p>If you click "Run !" and haven't clicked on "Select .txt file" first it will prompt you to select a file anyway.
You can access the self.txtFile variable from any method inside the 'Application' class.</p>

<pre><code>from __future__ import print_function
from Tkinter import *
from Tkinter import Tk
from tkFileDialog import askopenfilename

class Application(Frame):
    def __init__(self,parent,**kw):
        Frame.__init__(self,parent,**kw)
        self.txtFile = None
        self.presentGUI()

    def openFile(self):
        ##Tk().withdraw()
        self.txtFile = askopenfilename(defaultextension=".txt", filetypes=(("something", "*.txt"),("All Files", "*.*") ))
        print(self.txtFile)

    def Function(self):
        if self.txtFile == None:
            self.openFile()
        with open(self.txtFile) as fp, open(('c:/map/test.txt'), 'w') as fo:
            for line in fp:

                if ('Hello') in line:
                    content = line.strip() + " Hello detected "
                else:
                    content = line.strip()
                fo.write(content + "\n")


    def presentGUI(self):      
        # Buttons
        self.button1 = Button(self, text="Select .txt file", command=self.openFile)
        self.button2 = Button(self, text="Run !", width=28, command=self.Function)

        # grid
        self.button1.grid(row=1, column=1)
        self.button2.grid(row=3, columnspan=3)

if __name__ == '__main__':
    root = Tk()
    root.title("simulation")
    app = Application(root)
    app.grid()
    root.mainloop()
</code></pre>

python function variables tkinter