Luca838
Luca838

Reputation: 47

File explorer automatically opens in Tkinter Python program

When I run my python program, the file explorer automatically opens. I would like this to only happen when the user presses the "Open" button.

Above has been solved.

The issue I am currently having, in terms of encrypting or decrypting text, is that either the upload a file functionality works or the type a string functionality. Both do not work in the same program.

I would appreciate any help.

from tkinter import *
from tkinter import filedialog

class Caesar(Frame):
    LETTERS = "abcdefghijklmnopqrstuvwxyz"

    def UploadAction(self):
        filename = filedialog.askopenfilename(initialdir="/",
                                          title="Open File",
                                          filetypes=(("Text Files", "*.txt"), ("All Files", "*.*")))

        with open('caesartest.txt') as f:
        contents = f.read().splitlines()
        contents = ' '.join(map(str, contents))
        return contents

    def __init__(self, pencere):
        Frame.__init__(self, pencere)
        self.pencere = pencere

        Label(pencere, text="Enter your message: ", relief=GROOVE, width=20).place(x=20, y=30)
        self.Ent1 = Entry(pencere, width=30)
        self.Ent1.place(x=230, y=30)

        Label(pencere, text="Upload a .txt file: ", relief=GROOVE, width=20).place(x=20, y=80)
        Button(pencere, text="Open", relief=GROOVE, font="bold", command=self.UploadAction).place(x=230, y=80)
        self.Ent3 = Entry(pencere, width=24)
        self.Ent3.place(x=280, y=80)
        self.Ent3.insert(100, self.UploadAction())  

        Label(pencere, text="Enter key: ", relief=GROOVE, width=20).place(x=20, y=120)
        self.Ent2 = Entry(pencere, width=30)
        self.Ent2.place(x=230, y=120)

        Button(pencere, text="Encrypt", relief=GROOVE, font="bold", command=self.Encrypt).place(x=200, y=150)
        Button(pencere, text="Decrypt", relief=GROOVE, font="bold", command=self.Decrypt).place(x=280, y=150)

        Label(pencere, text="Result: ", relief=GROOVE, width=20).place(x=20, y=203)
        self.RESULT = Entry(pencere, width=30)
        self.RESULT.place(x=230, y=200)

    def Encrypt(self):
        key = int(self.Ent2.get())
        length = len(self.LETTERS)

        translation = ''

        text = self.Ent1.get()
        text = self.Ent3.get()
        text = re.sub('[^A-Za-z]+', '', text.lower())

        for character in text:
            if character in self.LETTERS:
                sayı = self.LETTERS.find(character)
                sayı = (sayı + key) % length
                translation += self.LETTERS[sayı]
            else:
                translation += character

        self.RESULT.delete(0, END)
        self.RESULT.insert(0, translation)

    def Decrypt(self):
        key = int(self.Ent2.get())
        length = len(self.LETTERS)

        translation = ''

        text = self.RESULT.get()
        text = re.sub('[^A-Za-z]+', '', text.lower())

        for character in text:
            if character in self.LETTERS:
                sayı = self.LETTERS.find(character)
                sayı = (sayı - key) % length
                translation += self.LETTERS[sayı]
            else:
                translation += character

        self.RESULT.delete(0, END)
        self.RESULT.insert(0, translation)

if __name__ == "__main__":
    root = Tk()
    root.title("Caesar")
    root.geometry("580x280+70+70")
    Caesar(root).pack(side="top", fill="both")
    root.mainloop()

Upvotes: 0

Views: 153

Answers (1)

furas
furas

Reputation: 142671

As for me problem is because you use

    self.Ent3.insert(100, self.UploadAction())   

inside __init__ so it executes UploadAction() at start, not when you press button.

You have to use insert inside UploadAction() which is executed by button.

def UploadAction(self):
    filename = filedialog.askopenfilename(initialdir="/",
                                      title="Open File",
                                      filetypes=(("Text Files", "*.txt"), ("All Files", "*.*")))

    if filename: 
        with open('caesartest.txt') as f:
            contents = f.read().splitlines()
            contents = ' '.join(map(str, contents))
        self.Ent3.insert(100, content) # <-- use it 

    # return contents # useless when used with `Button` 

def __init__(self, pencere):
    Frame.__init__(self, pencere)
    self.pencere = pencere

    Label(pencere, text="Enter your message: ", relief=GROOVE, width=20).place(x=20, y=30)
    self.Ent1 = Entry(pencere, width=30)
    self.Ent1.place(x=230, y=30)

    Label(pencere, text="Upload a .txt file: ", relief=GROOVE, width=20).place(x=20, y=80)
    Button(pencere, text="Open", relief=GROOVE, font="bold", command=self.UploadAction).place(x=230, y=80)
    self.Ent3 = Entry(pencere, width=24)

    self.Ent3.place(x=280, y=80) 

    # self.Ent3.insert(100, self.UploadAction()) # <-- don't do this

Upvotes: 1

Related Questions