I have updated your script with added functionality.
Can now use the carriage return to enter input as opposed to just using the mouse.
Auto focuses the pointer inside the entry box both at start-up and after every entry.
It is not recommended to use the open-ended import of: from tkinter import * for two main reasons:
a. tkinter names may clash with native names (user defined names)
b. Easier to interpret your code (maybe another user) by noting which names are from import packages and which are user defined. Thus, it is best practice to prefix imported package names with library name or alias.
Switched to using the with open file open statement. This method of opening a file always closes a file on exit automatically and thus is the preferred way of opening files. Other types require the pairing with an explicit close statement. If there is an exception, the âwith openâ method will still close the file.
Incorporating the pathlib library for defining the path and filename.
Added code to clear the entry box after every write.
I have tested this script on my system and it is working fine.
import tkinter as tk
import tkinter.messagebox as mbox
from pathlib import Path
file_path = Path('/Users/your_pc_name_here/Desktop/tester.txt')
win = tk.Tk()
win.title('Login')
none = ", "
frame = tk.Frame(win)
entry = tk.Entry(frame)
def login(*args):
with open(file_path, 'a+', encoding='UTF-8') as file:
file.write(entry.get())
file.write(none)
mbox.showinfo('Greet', 'Welcome ' + entry.get())
entry.delete(0, tk.END) # After writing to file, clears the entry box
entry.focus_set() # Refocuses cursor inside entry box after every write
btn = tk.Button(frame, text = 'Enter name to login.', command = login)
btn.bind("<Return>", login) # This adds the carriage return functionality to button
btn.pack(side = tk.RIGHT, padx = 5)
entry.pack(side = tk.LEFT)
frame.pack(padx = 20, pady = 20)
entry.focus_set() # Puts cursor inside entry at start-up
win.mainloop()
FileNotFoundError: [Errno 2] No such file or directory: âusers\puter\desktop\login.txtâ
Exception in Tkinter callback
Traceback (most recent call last):
File âc:\Users\silas\AppData\Local\Programs\Python\Python312\Lib\tkinter_init_.pyâ, line 1968, in call
return self.func(*args)
^^^^^^^^^^^^^^^^
File âC:\Users\silas\OneDrive\Desktop\login.pyâ, line 15, in login
with open(file_path, âa+â, encoding=âUTF-8â) as file:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: âusers\puter\desktop\login.txtâ
PS C:\Users\silas\OneDrive\Desktop>
âusers\puter\desktop\login.txtâ is a relative path. It starts in the current directory, wherever that is, then down into a subfolder called âusersâ, further down into a subfolder called âputerâ, further down into a subfolder called âdesktopâ, then to a file called âlogin.txtâ.
By the way, for the first item (1. ) added from the previous post, as soon as you enter the last character in the entry, then hit the Tab key then Enter.
If you want to use backward slashes, you will have to prefix the file path with the letter r as in:
Neos_Helios, Iâve tried out your code and it seems to work just fine. I can see the text gets appended to the file. Are you seeing any error messages when you run your original code?
Thatâs actually not quite right as the concern youâre bringing up only affects string literals. You you pass it in like the original code sample does from a tkinter input then the path separator style doesnât matter on Windows. I think the comment from @MRAB is correct.
using filemode âa+â is seldom useful. Reading and appending to a file is not clearly defined over different OSes. Since you are only writing, âaâ is sufficient. Keep your with-Block as small as possible.
def login(*args):
with open(file_path, 'a', encoding='UTF-8') as file:
file.write(entry.get() + none)
mbox.showinfo('Greet', f'Welcome {entry.get()}')
# After writing to file, clears the entry box
entry.delete(0, tk.END)
# Refocuses cursor inside entry box after every write
entry.focus_set()