0

Experiencing some trouble using TKinter in python. In slowly building what will be a simple program with a single master window and 4 tabs (notebooks), I have run into trouble determining how to pass a filename to function that needs it.

I have one button that opens a file dialog and allows the user to select a file and (hopefully) retrieve the file name. I have another button setup that will (hopefully) trigger a process, with the name of the chosen file passed as an argument to the method called in the command. Right now, I can trigger the open file dialog, and print the file name to the console, but I can not figure out how to pass this filename to the delineate command (which will ultimately trigger a different script with the filename as input to this script). I have it set up so that when 'Delineate' button is triggered, it displays 'Delineating....' on the window. My goal is to be able to display 'Delineating X', where X is the opened file's name, and pass this name on to another script. If I can get it to the point where 'Delineating X' is correctly displayed, I that means the filename has been passed and I think I can do the rest...

Here is the code:

from Tkinter import *
import ttk
import tkFileDialog
import tkMessageBox
import Tkconstants

class Unified_Tool(ttk.Frame):
    # =============================================================================
    # Main setup options
    def __init__(self, isapp=True, name='unified_tool'):
        ttk.Frame.__init__(self, name=name)
        self.master.title("Unified Tool")
        self.pack(expand=Y, fill=BOTH)
        self.isapp = isapp
        self._create_widgets()
        file_name = StringVar()

    def _create_widgets(self):
        self._create_panels()

    def _create_panels(self):
        panel = Frame(self, name='panel')
        panel.pack(side=TOP, fill=BOTH, expand=Y)
        # create the notebook
        nb = ttk.Notebook(panel, name='notebook_panel')
        nb.pack(fill=BOTH, expand=Y, padx=2, pady=3)
        self._create_ca_del_tab(nb)
        #self._create_traversal_tab(nb)
        #self._create_tot_tab(nb)
        #self._create_zcc_tab(nb)

    # =============================================================================
    # Delineation tab
    def load_file(self):
        file_name = tkFileDialog.askopenfilename(filetypes=([('All files', '*.*'),
                                                               ('Text files', '*.txt'),
                                                               ('CSV files', '*.csv')]))
        print file_name

    # =============================================================================
    # Delineation tab
    def _create_ca_del_tab(self, nb):

        # variable to store the filename
        del_filename = StringVar()

        # frame to hold content
        frame = ttk.Frame(nb, name='ca_delineation')

        # widgets to be displayed on 'Description' tab
        msg = ["For delineating the catchment area of a point or collection of points. In "
               "the file selection box to the left, select the input file containing the points "
               "for catchment area delineation."]
        lbl_intro = ttk.Label(frame, wraplength='4i', justify=LEFT, anchor=N,
                        text=''.join(msg))

        # button for selecting the input file
        btn_del_select_file = ttk.Button(frame, text="Browse", command=self.load_file, width=10)
        # button for triggering the task
        btn_del = ttk.Button(frame, text='Delineate!', underline=0,
                              command=lambda v=del_filename: self._delineate(del_filename))

        # label that displays the input file name
        lbl_del = ttk.Label(frame, textvariable=del_filename, name='delineate')

        # position and set resize behaviour
        lbl_intro.grid(row=0, column=0, columnspan=2, sticky='new', pady=5)
        lbl_del.grid(row=1, column=1,  pady=(2,4))
        btn_del_select_file.grid(row=1, column=0, pady=(2,4))
        btn_del.grid(row=2, column=0, pady=(2,4))
        frame.rowconfigure(1, weight=1)
        frame.columnconfigure((0,1), weight=1, uniform=1)

        # add to notebook (underline = index for short-cut character)
        nb.add(frame, text='CA Delineation', underline=0, padding=2)

    def _delineate(self, v):
        v.set('Delineating....')
        self.update()
2
  • Please change your class method. check this PDF google.com.tr/… Why are you trying to make things complicated? Nuke control system ? Commented Jan 15, 2016 at 7:29
  • I am building a window with four tabs - only working on the first tab right now. Does it seem to complicated for a US that will have four different tabs, all with their buttons/labels/actions? Commented Jan 15, 2016 at 8:53

2 Answers 2

2

Just make file_name to be an instance variable, instead of a temporary one:

def load_file(self):
    self.file_name = tkFileDialog.askopenfilename(filetypes=([('All files', '*.*'),
                                                           ('Text files', '*.txt'),
                                                           ('CSV files', '*.csv')]))
    print self.file_name

Then you'll have acess to self.filename anywhere within the Unified_Tool class.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your help. Actually whenever I'm trying to do so, in my code block, it only returns AttributeError: 'ClassName' object has no attribute 'fileName' . Kindly help me on this issue. Thanks in advance.
@Ozzius can you put the relevant sections of the code on pastebin.com for analisys?
1

Just set the global variable inside the load_file function and export the file_name to other functions like this:

def load_file(self):
    global csv_filename
    self.file_name = tkFileDialog.askopenfilename(filetypes=([('All files', '*.*'),('Text files', '*.txt'),('CSV files', '*.csv')])
    csv_filename = self.filename
    print self.file_name

From here onward, you can use csv_filename as the output of load_file function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.