Skip to main content
Tweeted twitter.com/StackCodeReview/status/1179637337975873537
Became Hot Network Question

Update 02/10/2019 on my digitized application Paper label system in Python

Source Link
DeveloperLV
  • 225
  • 3
  • 17

Update 02/10/2019 on my digitized application

Update from Digitizing paper label system with Python

I have implemented a username and password log in and main application to be used after successful log in.

I am ready for some more feedback.

My main troubles is error handling. For example log in screen, I'll need to implement statement to show if user does not exist etc.. Nothing too fancy. Also, maybe add a scroll bar and center the screen for both classes when opened up.

Also, if there is nothing inside a folder, the drop down menu dissapears and shows nothing, I'll need to work on that too. Maybe pop another screen saying 'No files in folder, come back later'.

Here is my full code:

from tkinter import *
from tkinter import DISABLED, messagebox
import tkinter.ttk as ttk
import os
import glob
from PIL import Image, ImageTk, ImageGrab
from pathlib import Path
import pyautogui
import time

def main():
    root = Tk()
    app = Window1(root)
    root.mainloop()

class Window1:
    def __init__(self,master):
        self.master = master
        self.master.title("User Log In")
        self.master.geometry('400x150')
        self.frame = Frame(self.master)
        self.frame.pack(fill="both", expand=True)


        self.label_username = Label(self.frame, text="Username: ",font=("bold",16))
        self.entry_username = Entry(self.frame, font = ("bold", 14))
        self.label_password = Label(self.frame, text="Password: ",font=("bold",16))
        self.entry_password = Entry(self.frame, show="*", font = ("bold", 14))

        self.label_username.pack()
        self.entry_username.pack()
        self.label_password.pack()
        self.entry_password.pack()

        self.logbtn = Button(self.frame, text="Login", font = ("bold", 10), command=self._login_btn_clicked)
        self.logbtn.pack()

    #close and stop tkinter running in backround, also see line #64
    def on_closing(self):
        self.master.destroy()


    def _login_btn_clicked(self):
        # print("Clicked")
        username = self.entry_username.get()
        password = self.entry_password.get()

        # print(username, password)
        account_list = [line.split(":", maxsplit=1) for line in open("passwords.txt")]
        # list of 2-tuples. Usersnames with colons inside not supported.
        accounts = {key: value.rstrip() for key, value in account_list}
        # Convert to dict[username] = password, and slices off the line ending.
        # Does not support passwords ending in whitespace.
        if accounts[username] == password:
            self.master.withdraw()
            self.newWindow = Toplevel(self.master)
            self.newWindow.protocol("WM_DELETE_WINDOW", self.on_closing)
            self.app = Window2(self.newWindow, window1 = self)
        else:
            messagebox.showinfo("User message", "Invalid username or password specified please try again")


class Window2:
    def __init__(self,master, window1):
        notebook = ttk.Notebook(master)

        notebook.pack(expand = 1, fill = "both")
        #Frames
        main = ttk.Frame(notebook)
        manual = ttk.Frame(notebook)
        notebook.add(main, text='Main-Screen')
        notebook.add(manual, text='Manual')
        self.window1 = window1


        def clock():
            t=time.strftime('%d/%m/%Y, %H:%M:%S, ',time.localtime())
            if t!='':
                self.display_time.config(text=t,font='times 15')
            main.after(100,clock)
        self.display_time=Label(main)
        self.display_time.grid(column = 3, row = 0)
        clock()


        username = self.window1.entry_username.get()
        self.User = Label(main, text = 'User: '+ username, font = ('15'))
        self.User.grid(column = 4, row = 0)


        var1 = IntVar()
        var2 = IntVar()
        var3 = IntVar()
        var4 = IntVar()
        var5 = IntVar()
        var6 = IntVar()
        var7 = IntVar()
        var8 = IntVar()
        var9 = IntVar()
        var10 = IntVar()
        var11 = IntVar()
        var12 = IntVar()

        #Displaying checkboxes and assigning to variables

        self.Checkbox1 = Checkbutton(main, text="Ingredients present in full (any allergens in bold with allergen warning if necessary)", variable=var1)
        self.Checkbox1.grid(column = 2, row = 1, sticky = W)

        self.Checkbox2 = Checkbutton(main, text="May Contain Statement.", variable=var2)
        self.Checkbox2.grid(column = 2, row = 2, sticky = W)

        self.Checkbox3 = Checkbutton(main, text="Cocoa Content (%).", variable=var3)
        self.Checkbox3.grid(column = 2, row = 3, sticky = W)

        self.Checkbox4 = Checkbutton(main, text="Vegetable fat in addition to Cocoa butter", variable=var4)
        self.Checkbox4.grid(column = 2, row = 4, sticky = W)

        self.Checkbox5 = Checkbutton(main, text="Instructions for Use.", variable=var5)
        self.Checkbox5.grid(column = 2, row = 5, sticky = W)

        self.Checkbox6 = Checkbutton(main, text="Additional warning statements (pitt/stone, hyperactivity etc)", variable=var6)
        self.Checkbox6.grid(column = 2, row = 6, sticky = W)

        self.Checkbox7 = Checkbutton(main, text="Nutritional Information Visible", variable=var7)
        self.Checkbox7.grid(column = 2, row = 7, sticky = W)

        self.Checkbox8 = Checkbutton(main, text="Storage Conditions", variable=var8)
        self.Checkbox8.grid(column = 2, row = 8, sticky = W)

        self.Checkbox9 = Checkbutton(main, text="Best Before & Batch Information", variable=var9)
        self.Checkbox9.grid(column = 2, row = 9, sticky = W)

        self.Checkbox10 = Checkbutton(main, text="Net Weight & Correct Font Size.", variable=var10)
        self.Checkbox10.grid(column = 2, row = 10, sticky = W)

        self.Checkbox11 = Checkbutton(main, text="Barcode - Inner", variable=var11)
        self.Checkbox11.grid(column = 2, row = 11, sticky = W)

        self.Checkbox12 = Checkbutton(main, text="Address & contact details correct", variable=var12)
        self.Checkbox12.grid(column = 2, row = 12, sticky = W)


        #PrintScreen
        def PrintScreen():
            pyautogui.keyDown('alt')
            pyautogui.keyDown('printscreen')
            pyautogui.keyUp('printscreen')
            pyautogui.keyUp('alt')
            self.dataSend['state'] = 'normal'

        def var_states():
            text_file = open("logfile.txt", "a")
            text_file.write("Username: [%s], option 1: [%d], option 2: [%d], option 3: [%d], option 4: [%d], option 5: [%d], option 6: [%d], option 7: [%d], option 8: [%d], option 9: [%d], option 10: [%d], option 11: [%d], option 12: [%d], Original Sign Off: [%s]\n" % (username,var1.get(),  var2.get(),  var3.get(),  var4.get(),  var5.get(),  var6.get(),  var7.get(),  var8.get(),  var9.get(),  var10.get(),  var11.get(),  var12.get(), self.p))
            text_file.close()
            self.img = ImageGrab.grabclipboard()
            self.img.save("%s" % ('//SERVER/shared_data/Technical/Label Sign Off Sheets/sign off project/Signed/'+ os.path.basename(self.p).strip('- to sign.jpg') + ' ' + username+ '.jpg'), 'JPEG')
            ed = ('//SERVER/shared_data/Technical/Label Sign Off Sheets/sign off project/Signed/'+ os.path.basename(self.p).strip('- to sign.jpg') +' ed'+ '.jpg')
            Nb = ('//SERVER/shared_data/Technical/Label Sign Off Sheets/sign off project/Signed/'+ os.path.basename(self.p).strip('- to sign.jpg') +' Nb'+ '.jpg')
            jj = ('//SERVER/shared_data/Technical/Label Sign Off Sheets/sign off project/Signed/'+ os.path.basename(self.p).strip('- to sign.jpg') +' jj'+ '.jpg')
            kl = ('//SERVER/shared_data/Technical/Label Sign Off Sheets/sign off project/Signed/'+ os.path.basename(self.p).strip('- to sign.jpg') +' kl'+ '.jpg')

            if os.path.exists(ed) and os.path.exists(Nb) or os.path.exists(jj) or os.path.exists(kl):
                os.remove('//SERVER/shared_data/Technical/Label Sign Off Sheets/sign off project/To sign/' + os.path.basename(self.p))
            else:
                print("False")





        self.dataSend = Button(main, text = "Send", command = var_states, state = DISABLED)
        self.dataSend.grid(column = 1, row = 13, sticky = W)
        self.CaptureScreen = Button(main, text = "PrintScreen", command = PrintScreen, state = DISABLED)
        self.CaptureScreen.grid(column = 1, row = 14, sticky = W)

###################################################################################################################################
                                                        ##Load Image##
###################################################################################################################################
        # Create a Tkinter variable
        self.tkvar = StringVar()

        # Directory
        self.nonedisplayed = "N/A"
        self.directory = "//SERVER/shared_data/Technical/Label Sign Off Sheets/sign off project/To Sign"
        self.choices = glob.glob(os.path.join(self.directory, "*- to sign.jpg"))



        self.tkvar.set('...To Sign Off...') # set the default option

        # Images
        def change_dropdown():
            imgpath = self.tkvar.get()
            img = Image.open(imgpath)
            photo = ImageTk.PhotoImage(img)
            label2.image = photo
            label2.configure(image=photo)
            self.CaptureScreen['state'] = 'normal'


        #return path value
        self.p = None
        def func(value):
            global p
            self.p = Path(value)
            print(self.p)

        #widgets
        self.msg1 = Label(main, text = "Choose here")
        self.msg1.grid(column = 0, row = 0)
        self.popupMenu = OptionMenu(main, self.tkvar, *self.choices, command = func)
        self.popupMenu.grid(row=1, column=0)
        self.display_label = label2 = Label(main, image=None)
        self.display_label.grid(row=2, column=0, rowspan = 500)
        self.open_button = Button(main, text="Open", command=change_dropdown)
        self.open_button.grid(row=502, column=0)

###################################################################################################################################
                                                        ##TAB 2 - MANUAL##
###################################################################################################################################

        def manualopen():
            os.startfile('//SERVER/shared_data/Technical/Food Safety & Quality Manual/Section 21 - Process Control/21.LABL.02 - Labelling notes.docx')

        self.manualBtn = Button(manual, text= "open doc", command = manualopen)
        self.manualBtn.pack()




if __name__ == '__main__':
    main()

Also, my passwords.txt file looks like this:

ed:password1
Nb:rother
jj:4spaces
kl:timepass