0

This is a follow-up question to this: Tkinter: print (.txt) filepath to text widget AND (.txt) file content to scrolledtext widget in the same gui with one filedialogue access

I have almost, sort-of working code, and a main function within it, is this:

def process(*args):
    try:
        # COMMENT: Reset output
        rdiff.set('')
        tdiff.set('')
        rtmiss.set('')

        
        ref: TextIO = open(filedialog.askopenfilename(title='add Reference file', initialdir='src', ), 'r')
        cfg: TextIO = open(filedialog.askopenfilename(title='add file to test', initialdir='src', ), 'r')
        reflines = ref.readlines()
        cfglines = cfg.readlines()

        # COMMENT: Comparing lines
        refhash = dict()
        cfghash = dict()
        different = False

        for i, value in enumerate(reflines):
            refhash[i] = value
        for i, value in enumerate(cfglines):
            cfghash[i] = value
            contains = value in refhash.values()
            different = different or not contains
            if not contains:
                tdiff.set(tdiff.get() + 'line ' + str(i) + ': ' + value)
        for i, value in enumerate(reflines):
            contains = value in cfghash.values()
            different = different or not contains
            if not contains:
                rdiff.set(rdiff.get() + 'line ' + str(i) + ': ' + value)
        if not different:
            errmsg.set('no differences')

        if len(reflines) == len(cfglines) and different:
            for i, _ in enumerate(reflines):
                if cfglines[i] != reflines[i]:
                    rtmiss.set(rtmiss.get() + 'line ' + str(i) + '\n')
        else:
            rtmiss.set('files have different number of lines')

        Label(frame_main, text="put file content in the text box below", anchor='w', bg='#abdbe3').pack(anchor='s',
                                                                                                        side=BOTTOM,
                                                                                                        fill=Y)
        scroll_w = 10
        scroll_h = 5
        myscrolledtext1 = scrolledtext.ScrolledText(frame_main, width=scroll_w, height=scroll_h, wrap=tk.WORD,background='#bbffe6')
        myscrolledtext1.vbar.config(width=20)
        myscrolledtext1.pack(anchor='s', fill=X, side=LEFT, ipadx=5,ipady=5)
        myscrolledtext1.insert(tk.END, rdiff.get())

        myscrolledtext2 = scrolledtext.ScrolledText(frame_main, width=scroll_w, height=scroll_h, wrap=tk.WORD, background='#bbffe6')
        myscrolledtext2.vbar.config(width=20)
        myscrolledtext2.pack(anchor='s', fill=X, side=LEFT, ipadx=5,ipady=5)  # .grid(sticky='news', row=12, column=2, rowspan=1, columnspan=1, padx=5, pady=5)
        myscrolledtext2.insert(tk.END, tdiff.get())

        myscrolledtext3 = scrolledtext.ScrolledText(frame_main, width=scroll_w, height=scroll_h, wrap=tk.WORD,background='#bbffe6')
        myscrolledtext3.vbar.config(width=20)
        myscrolledtext3.pack(anchor='s', fill=X, side=LEFT, ipadx=5,ipady=5)  # .grid(sticky='news', row=12, column=3, rowspan=1, columnspan=1, padx=5, pady=5)
        myscrolledtext3.insert(tk.END, rtmiss.get())

    except OSError:
        errmsg.set('failed to load both files')

and the other functions:

def fd_getref(ref):
    Label(frame_main, anchor='w', width=80, text='Reference filepath:', background='#abdbe3').pack(side=BOTTOM, fill=Y)
### ref: TextIO = open(filedialog.askopenfilename(title='add Reference file', initialdir='src', ), 'r')
    refname = ref.name
    refread = ref.read()
    refname_new = refname.replace('/', '\\')
    pathtext_ref = Text(frame_main, width=80, height=1, bg='#f0f0f0')
    pathtext_ref.pack(anchor='n', side=TOP, fill=Y, ipadx=5, ipady=5)#.grid(row=3, column=1, rowspan=1, columnspan=3, padx=5, pady=5, sticky='w')
    pathtext_ref.configure(font=("Arial", 9, "italic",))
    pathtext_ref.insert(tk.END, refname_new)

def fd_getcfg(cfg):
    Label(frame_main, anchor='w', width=80, text='Test filepath:', background='#abdbe3').pack(side=BOTTOM, fill=Y)

    cfgname = cfg.name
    cfgread = cfg.read()
    cfgname_new = cfgname.replace('/', '\\')
###: TextIO = open(filedialog.askopenfilename(title='add file to test', initialdir='src', ), 'r')
    pathtext_cfg = Text(frame_main, width=80, height=1,  bg='#f0f0f0')
    pathtext_cfg.pack(anchor='n', side=TOP, fill=Y, ipadx=5, ipady=5)#.grid(row=6, column=1, rowspan=1, columnspan=3, padx=5, pady=5, sticky='w')
    pathtext_cfg.configure(font=("Arial", 9, "italic"))
    pathtext_cfg.insert(tk.END, cfgname_new)


This as a function, does everything required, but, the problem is that it used to be 3 functions.

1 function to get one file, and read and print the path

1 function to get another file, and read and print the path

1 function to run the process on two files

When I had it split apart like this, one or more function would complain about not having positional arguments.

Part of this problem is about the opening and reading of files. By bundling it all in one function, along with the scrolled text boxes, I sacrificed the path reading and printing to get the end result, because the path display is just a nice to have... but really, until I can make it do that, I'm not going to be able to build more complex GUIs doing more things with data, where being able to display what's happening, and allow user interaction, is part of the functionality.

The question is, how to get this to work without trying to open and reopen files? There must be a way of reading and passing the name inside the TextIO or the text tile content between functions to create a compartmentalised wizard.

Ultimately, I want to move from breaking it up into functions, to breaking it up into classes, and then files; to understand the process of building a small idea into a bigger one, where you likely need to have more compartmentalised (and reusable) code to pass data around to perform actions on it.

1 Answers1

0

I solved this by using stringvars outside the functions to pass the variable into the stringvar inside one function, and then out again in another function, but encountered problems with formatting the content of the textfiles, which I answer on the next question about this.

https://stackoverflow.com/posts/73560525