Now that we’ve covered the basics of Tkinter and how to read and write files in Python, it’s time to start piecing together the beginning of our code. ʕ•ᴥ•ʔ

Since we’ll be getting information from the user, we need to understand how this is done using Tkinter in Python.

First, let’s define a function. We’ll call it ‘add’. In Python functions are usually defined using the def keyword. We’re naming this function add because its purpose is to store usernames and passwords in a text file.

Having a function is important because it acts as an independent unit of code. This means it can be tested separately without affecting the rest of the program.  Along with the fact  functions can be called anywhere, making the code more efficient and easier to manage, especially in large projects.

Now for getting values from the user. We would get the user’s input through the ‘get()’ method. This is an entry widget within tkinter to get single line text input from the user. We would also need to create an input field for the user within the GUI.

Label

The way that I did it was creating the input box for the user. We would be using something called a ‘label widget’. This is the way i have done it:

  • tk.Label(app, text=”USERNAME:”) – Creates a label displaying “USERNAME:” in the app window.
  • .grid(row=0, column=0, padx=15, pady=15) – Positions the label in row 0, column 0, with padding for spacing.
  • tk.Entry(app) – Creates an entry field for user input.
  • .grid(row=0, column=1, padx=15, pady=15) – Positions the entry field in row 0, column 1, next to the label.

Let’s look at the output now:

Looks good! Now we just need to do the same for the password label. It follows the exact same process, we just change the label name and adjust the positioning of the field accordingly.

We should also test whether the input fields work by typing in them to ensure everything functions as expected.

Now let’s get the user input sorted out.

We need to understand how this would be done since we are using Tkinter, not the actual core Python input function. While programming languages share similar concepts, we typically collect user input and store it in a variable.

We would be using the ‘.get()’ function to do this. The way that we would structure this would be by ‘variablename = labelname.get()’. Like this:

  • entryName.get() – gets the text entered by the user in the entryName input field.
  • username = entryName.get() – Stores the inputted text in the variable username for further use in the program.

Inputting data into text file

Okay, so what’s next? Well, judging by the title, you can probably guess we need somewhere to store the data, like a text file! Very safe and secure, don’t worry.

Ideally, usernames and passwords should be stored in a secure database with proper encryption, but that’s why this is KaiPass v1, a simple first version. For now we’ll stick to writing the inputted data into a text file before exploring more secure methods in later versions.

Using what we understood from the previous blog, we would need to open the text file, write the username and password at the bottom of what’s already written and make sure that isn’t cleared. So we would be using the mode ‘a’.

This also creates the file for us, so we don’t need to do that either. Using the ‘writing’ format, this is what we get.

  • with open(“passwords.txt”, ‘a’) as f: – Opens (and creates the file because it doesn’t exist) a file named “passwords.txt” in append mode (‘a’), meaning new data is added to the end of the file without deleting existing content.
  • f.write(f”{username} {password}\n”) – Writes the username and password to the file, separated by a space, and adds a new line (\n) to ensure each entry appears on a new line.

But what happens if the user only inputs just a username? Or only a password? or nothing? We need validation. The most simple way to do this is through ‘if’ and ‘else’ statements. 

If username and password:

Input username and password in text file

Print “Successfully inputted”

Else

Print “ please enter both the fields”

We would use the ‘messagebox’ as a way to inform the user if they have done things correctly or not. We can also use the revelevent icons as well.  

  • if username and password: – Checks if both username and password are provided (make sure that the fields are not empty).
  • If both fields are filled (true):
    • Inputs the values into the text file
    • messagebox.showinfo(“Success”, “Password added !!”) – Displays a success message in a popup box.
  • If any field is empty (else_:
    • messagebox.showerror(“Error”, “Please enter both the fields”) – Shows an error message, prompting the user to fill both fields.

The submit button 

Now we need to create a submit button for the user. This button would correspond to the function that would  be used, to ensure that function would be used! We use a similar  way in creating the label. 

  • tk.Button(app, text=”Add”, command=add) – Creates a button labeled “Add” inside the Tkinter app. When clicked, it calls the add function, which handles saving the username and password.
  • .grid(row=2, column=0, padx=15, pady=8, sticky=”we”) – Positions the button

Now let’s try see if it works

It works! ʕ•ᴥ•ʔ🎉

Now what! Probably test what happens if there isn’t any text inputted within the field, then we should be done for this function. Here we can see that the if function does work:

The next blog would be able the getlist() function, where the user can see the username and passwords that are already within the text file. Now let’s code ‘em all! ʕ -ᴥ•ʔ♡

Resources:

+

Leave a comment