Jan-18-2022, 03:51 PM
Hi All,
I have been testing a small program for a little while now and recently noticed that the program was not logging any data to my text file. For some reason, if I start out with deleting the file, it will start logging. This has happened to me a couple of times, but I cannot see anything that looks wrong in my code for appending to the text file. If the file does not write, I do not get any error messages of any kind and everything else about it works as expected.
I have been testing a small program for a little while now and recently noticed that the program was not logging any data to my text file. For some reason, if I start out with deleting the file, it will start logging. This has happened to me a couple of times, but I cannot see anything that looks wrong in my code for appending to the text file. If the file does not write, I do not get any error messages of any kind and everything else about it works as expected.
#!/usr/bin/env python3
import tkinter as tk
import tkinter.ttk as ttk
import RPi.GPIO as GPIO
from tkinter import *
import time
import shutil
from datetime import datetime
from tkinter import messagebox, filedialog
GPIO.setmode(GPIO.BCM)
GPIO.setup(5, GPIO.IN)
GPIO.setup(6, GPIO.IN)
class App(Frame):
def __init__(self,master=None):
Frame.__init__(self, master)
root.attributes('-fullscreen', True)
button1 = Button (master, text = "COPY ESD", command=lambda: self.esd_file())
button1.place(x=275, y=285)
button = Button (master, text = "EXIT ESD", command = quit)
button.place(x=380, y=285)
tk.Label(master, text="Badge #",font=("Helvetica", 30)).grid(row=0)
tk.Entry(root).place(x=170,y=10, width=200, height=40)
self.master = master
self.label = Label(text="", fg="blue", font=("Helvetica", 30))
self.label.place(x=40,y=150)
self.entry_widget = tk.Entry(root, font=("Helvetica", 26))
self.entry_widget.focus_set()
self.entry_widget.place(x=170,y=10, width=200, height=40)
self.update_clock()
def update_clock(self):
d = datetime.now().strftime("%m-%d-%Y %H:%M:%S")
newT = datetime.strptime(d, "%m-%d-%Y %H:%M:%S").strftime("%m-%d-%Y %I:%M:%S %p")
now = time.strftime("%H:%M:%S")
if not self.entry_widget.get():
self.label.configure(text=newT + '\n' + '\n'"NOT READY / SCAN BADGE", fg="red", font=("Helvetica", 24))
self.label.place(x=30,y=100)
self.after(1000, self.update_clock)
else:
self.label.configure(text=newT + '\n' + '\n'" TESTER READY! ", fg="blue", font=("Helvetica", 24))
self.label.place(x=60,y=100)
self.after(1000, self.update_clock)
if GPIO.input(5) == 1 and len(self.entry_widget.get()) > 6:
text_entered = self.entry_widget.get()
self.label.configure(text="PASS",fg="green", font=('Helvetica 110 bold'))
self.label.place(x=48,y=100)
self.after(20000, self.update_text)
FileName = str("/home/pi/esd.txt")
with open(FileName, "a") as f: # open file
f.write("Badge# " + text_entered + ", Test PASSED ON: " + newT + '\n')
self.entry_widget.selection_range(0, END)
elif GPIO.input(6) == 1 and len(self.entry_widget.get()) > 6:
text_entered = self.entry_widget.get()
self.label.configure(text="FAIL",fg="red", font=('Helvetica 110 bold'))
self.label.place(x=80,y=100)
self.after(20000, self.update_text)
FileName = str("/home/pi/esd.txt")
with open(FileName, "a") as f: # open file
f.write("Badge# " + text_entered + ", Test FAILED ON: " + newT + '\n')
self.entry_widget.selection_range(0, END)
def update_text(self):
self.entry_widget.delete(0, 'end')
def esd_file(self):
try:
shutil.copyfile(src="/home/pi/esd.txt", dst="/media/pi/ESD-USB/esd/esd.txt")
messagebox.showinfo('information', 'Transfer Complete!')
except IOError as e:
messagebox.showerror('information', 'Transfer Failed!')
def quit(self):
root.destroy()
root = Tk()
app=App(root)
root.wm_title("ESD TESTER")
root.geometry("520x480")
root.after(1000, app.update_clock)
root.mainloop()
