Oct-20-2021, 02:01 PM
Hello All... I am new to this forum and quite new to Python, please forgive my ignorance. I am using this code for a basic Tkinter GUI and need to figure out how to properly exit the code if my entry widget is blank. I am using 2 GPIO inputs on a Raspberry Pi along with this GUI. In my "def update_clock" I am checking if the entry is there or not and simply changing the label based on that. What I want to do is, "IF" the entry widget is blank and GPIO.input (5) == 1, I want to override everything and still keep a message "TESTER NOT READY". Getting the status of the GPIO is a physical pushbutton. Not sure how to handle this. Please let me know if I am making no sense at all here.
#!/usr/bin/env python3 import tkinter as tk import tkinter.ttk as ttk import RPi.GPIO as GPIO from tkinter import * from tkinter import messagebox import time from datetime import datetime 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) button = Button (master, text = "EXIT ESD", command = quit) button.place(x=375, y=280) 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! ", fg="red", font=("Helvetica", 24)) self.label.place(x=60,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: 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(40000, 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') elif GPIO.input(6) == 1: 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(50000, 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') def update_text(self): self.entry_widget.delete(0, 'end') 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()