Icsilk
Icsilk

Reputation: 567

displaying file name, not content in text widget tkinter Python

I've no idea why I haven't been able to find a good solution to this problem yet, it seems very elementary to me .. though not elementary enough to figure it out satisfactorily. A chapter project in a cryptology book Im reading instructs to write a simple mono-alphabetic cipher in your preferred language ... I chose Python.

It starts with a simple tkinter app. with some widgets, lol ... duh. Anyways here's the relevant code:

from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter.messagebox import showerror


class Application(Frame):
    def __init__(self, master):
        """ Initialize Frame. """
        super(Application, self).__init__(master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        """ Set all program widgets. """
        # set all labels
        Label(self, text = "Plaintext File: ")\
            .grid(row=0, column=0, sticky=W)
        Label(self, text = "Ciphertext: ")\
            .grid(row=3, column=0, sticky=W)
        Label(self, text = "Offset: ")\
            .grid(row=2, column=0, sticky=W)

    # set buttons
    Button(self, text = "Browse", command=self.load_file, width=10)\
        .grid(row=1, column=0, sticky=W)

    # set entry field
    self.file_name = Text(self, width=39, height=1, wrap=WORD)
    self.file_name.grid(row=1, column=1, columnspan=4, sticky=W)

    # set display field
    self.output_display = Text(self, width=50, height=5, wrap=WORD)
    self.output_display.grid(row=4, column=0, columnspan=4, sticky=W)

    # set offset amount spinbox
    self.offset_amt = IntVar()

    self.offset_amt = Spinbox(self, from_=1, to=13)
    self.offset_amt.grid(row=2, column=1, sticky=W)

    # set shift direction
    self.shift_dir = StringVar()
    self.shift_dir.set('r')

    Radiobutton(self, text="Shift Right", variable=self.shift_dir, value='r')\
        .grid(row=2, column=2, sticky=W)
    Radiobutton(self, text="Shift Left", variable=self.shift_dir, value='l')\
        .grid(row=2, column=3, sticky=W)


def load_file(self):

    self.filename = askopenfilename(initialdir='~')


    if self.filename: 
        try: 
            #self.settings.set(self.filename)
            self.file_name.delete(0.0, END)
            self.file_name.insert(0.0, open(self.filename, 'r'))
        except IOError: 
            showerror("Open Source File", "Failed to read file \n'%s'"%self.filename)
            return


def main():
    root = Tk()
    root.title("simple mono-alpha encrypter")
    root.geometry('450x250')
    app = Application(root)

for child in app.winfo_children(): 
    child.grid_configure(padx=3, pady=3)

root.mainloop()

main()

There's only a very little of it that actually does anything besides create widgets right now, I decided to post it all since its not that involved yet and someone can get a good idea of where Im at.

My problem that I haven't solved is that when I press use the 'Browse' button to choose a file to encrypt and then choose the file, the file content is displayed in the 'file_name' text widget, rather than the file name itself.

Im thinking that I have to change the 'filename' variable to not the actual file name but the file instead and then load the content of the File Name field from the open file dialog box in a 'filename' variable. I just haven't been able to figure out how to do that yet. Nor have I come across an appropriate method to do it. Any guidance??

Thanks F

Upvotes: 2

Views: 2738

Answers (1)

gfortune
gfortune

Reputation: 2609

Displaying the Filename

self.file_name.insert(0.0, self.filename)

Displaying the File Contents

You just need to read the data in from the file. See http://docs.python.org/library/stdtypes.html#file-objects

with open(self.filename, 'r') as inp_file:
    self.file_name.insert(0.0, inp_file.read())

Upvotes: 5

Related Questions