0

I'm trying to call these 3 class variables from another script, by importing the calls onto the new script. This is how it's suppose to work, when the logIn.py runs if you typed the correct user and password, they're saved in user and password variables. if you fail, 1 is added to the fail variable. At 1st I had the like this fail = 0, user = "", password = "" cause I was getting undefined errors. Everything worked, but when I create an object of that class, everything was again initialized to fail = 0, user = "", password = "". So I thought "maybe if I use StringVar and IntVar that'll fix the problem". But instead I get this error.

Traceback (most recent call last):
  File "C:\Users\devilboy 4\Documents\Visual Studio 2013\Projects\mainPage3\mainPage3\logIn_screen.py", line 9, in <module>
    class checkValidation:
  File "C:\Users\devilboy 4\Documents\Visual Studio 2013\Projects\mainPage3\mainPage3\logIn_screen.py", line 11, in checkValidation
    fail = IntVar()
  File "C:\Python31\lib\tkinter\__init__.py", line 265, in __init__
    Variable.__init__(self, master, value, name)
  File "C:\Python31\lib\tkinter\__init__.py", line 174, in __init__
    self._tk = master.tk
AttributeError: 'NoneType' object has no attribute 'tk'

I'm pretty sure it's related to the String,IntVar. If this works properly, on mainPage3 I'll generate a report and these 3 variables are some the ones I'll use.

logIn_screen.py:

from tkinter import *

import os

import tkinter.messagebox


#check si lo entrado es correcto
class checkValidation:

    fail = IntVar()

    user = StringVar()

    password = StringVar()

     #valida el user y el pass
    def fullVali(self, name, passwd):

        if  name == "" or name == " ":

            tkinter.messagebox.showinfo( "Error","Dejo el usuario en blanco")

            self.fail+= 1

        elif name != "UmetSeg":

            tkinter.messagebox.showinfo( "Error","Usuario incorrecto")

            self.fail+= 1

        else :

            self.user = name

            tkinter.messagebox.showinfo( "ok","dude" + name)


        if  passwd == "" or passwd == " ":

            tkinter.messagebox.showinfo( "Error","Dejo la password en blanco")

            self.fail+= 1

        elif passwd != "SegUmet":

            tkinter.messagebox.showinfo( "Error","Password incorrecto")

            self.fail+= 1

        else :

            self.password = passwd

            tkinter.messagebox.showinfo( "ok","dude" + passwd)

            form.destroy()


             #open another py script    
            os.system("mainPage3.py 1")

            return

     # no deja pasar parametros por command en el boton a menos que se por lambda, so corre este metodo para 
     #correr el metodo de validar
    def callVali(self):

        user = usrIn.get()

        self.fullVali(usrIn.get(), passIn.get())

        return

    def getUser(self):

        return self.user

    def getPass(self):

        return self.password


#este if es para que corra esto si se corre el programa y no si se importa la clase
if __name__ == "__main__":

    vali = checkValidation()

    form = Tk()

    form.title("LogIn")

    form.geometry("300x320+300+200")


    #User txtBox
    usrIn = Entry(form, textvariable = None, width = 30)

    usrIn.place(x = 60, y = 140)

    user = usrIn.get()

    #Passwd txtBox - show es para que se vea eso pero el texto sea lo que se escribe
    passIn = Entry(form, show = "*", textvariable = None, width = 30)

    passIn.place(x = 60, y = 200)

    #Username Label
    usrLblVal = StringVar()

    usrLblVal.set("User name")

    usrLbl = Label(form, textvariable = usrLblVal  )

    usrLbl.place(x = 120, y = 115)


    #Passwrd label
    passLblVal = StringVar()

    passLblVal.set("Password")

    passLbl = Label(form, textvariable = passLblVal  )

    passLbl.place(x = 120, y = 175)


    #Login btn
    btn = Button(form, text = "Entrar", width = 10, command = vali.callVali)

    btn.place(x = 110, y = 250)



    form.mainloop()

mainPage3.py(where I import class from logIn_screen.py):

from tkinter import *

from logIn_screen import checkValidation

import os

import time

import tkinter.messagebox

#stuff to do:
#seguir el formato hechoen writeFile.py
#bregar con lo de procesar imagenes
#cambiar los botones de help, reporte y logout por imagenes enves de texto
#ponerle un background image a todo para que se vea mas bonito

#Reporte shit:
#se genera un reporte cadavez que se hace un fail attempt a entrar en el login, cadavez que se abre esta pagina(con hora fecha y dia), 
#cada vez que se presiona editar(sale el valor), cadavez que se captura y se presiona Si(sale el resultado) o No y cadavez que se presione logOut(sale fecha y hora) .

#seguir con el reporte.

#tiene todos los metodos que bregan con agregar widgets y quitarlos
class formManipulation:
    # total default de parkings
    tot = 200

    # 0 = no 1 = yes
    totChanged = 0

    cv = checkValidation()

    # 0 = no 1 = yes
    imgAceptada = 1

    #tiempo con formato 12     
    t1 = time.strftime("%I:%M%p")

    #fecha
    d1 = time.strftime("%d-%m-%Y")

    # tiempo cuando se undio logOut
    #t2 = ""

    ##fecha al undirse logOut
    #d2 = ""





    #corre cuando se unde actualizar y cambia el valor de parking en total
    def changeTotal(self, txt):

        self.totChanged = 1

        if txt == "" or txt == " ":
           tkinter.messagebox.showinfo( "Error","Dejo el total de estacionamientos en blanco")

        else:

            try:
                self.tot = int(txt)

            except:

                tkinter.messagebox.showinfo( "Error","El valor debe ser numerico")

            if self.tot < 1:

                tkinter.messagebox.showinfo( "Error","El valor debe ser mayor a cero")

            else:

                self.tot = str(self.tot)

     #se usa para llamar changeTotal(self, txt) por que esta mierda no se le pueden pasar parametros por el command del buton
    def callChange(self):

        self.changeTotal(txtBoxPark.get())


     #el form peque~o(no puedo usar e~e ni que sale un error...) que sale despues de presionar capturar
    def askForm(self):

        self.imgAceptada = 1

        #display new form
        form2 = Tk()

        form2.title("Cotego de imagen")

        form2.geometry("300x100")

        newBox = LabelFrame(form2, text = "La imagen es correcta?", height = "50", width = "250")

        newBox.place(x = 30, y = 17)

         #btn Si
        btnY = Button(newBox, text = "Si", width = 3, command = self.changeDisTxt)

        btnY.place( x = 50)


        #btn No
        btnN = Button(newBox, text = "No", width = 3, command = self.killImg)

        btnN.place(x = 150)


       #display la cantidad sobrante de parkings de acuerdo al total en el txtBox de estacionamientos
    def changeDisTxt(self):

        #puse esto aqui envez de en la classe de reporte por que pasar parametros
        #por widgets de tkinter es una jodienda.
        with open(self.d1 + ".txt", "a+") as f:

            f.write("--Imagen capturada-- \r\n\r\n"+

       "Correcta: Si\r\n\r\n" +

       "Path de la imagen:  \r\n\r\n" +

       "Disponibilidad: 50/"+ self.tot + "\r\n\r\n" +

        self.d1 + " " + self.t1 + "\n" +
        "--------------------------------\r\n\r\n")

        if self.imgAceptada > 0:

            txtBoxDisp.insert(0, "50/" + self.tot)

            txtBoxDisp.configure(state = "readonly")

        else:

            tkinter.messagebox.showinfo( "Error","Debe capturar una nueva imagen")



        #desaprace la foto
    def killImg(self):

        #puse esto aqui envez de en la classe de reporte por que pasar parametros
        #por widgets de tkinter es una jodienda. 
        with open(self.d1 + ".txt", "a+") as f:

            f.write("--Imagen capturada-- \r\n\r\n"+

       "Correcta: No\r\n\r\n" +

       "Path de la imagen:  \r\n\r\n" +

        self.d1 + " " + self.t1 + "\n" +
        "--------------------------------\r\n\r\n")

            self.imgAceptada = 0

        lblImg.pack_forget()       


        #display la foto
    def displayImg(self):        

        lblImg.pack()

        self.askForm()

        #llama al script que desplega el howTo
    def openHelp(self):

        os.system("howTo.py 1")

        #grava la fecha y tiempo que se presiono logOut y abre el script de logIn
    def openLogIn(self):

        #tiempo con formato 12     
        t2 = time.strftime("%I:%M%p")

        #fecha
        d2 = time.strftime("%d-%m-%Y")

        failStr = str(self.cv.fail)

        totStr = str(self.tot)

        with open(self.d1 +".txt", "a") as f:

            f.write("--Reporte Final-- \r\n\r\n"+

            "Entrada: "+ self.d1 + " " + self.t1 +" \r\n" +
            "Intentos fallados: " + failStr + " \r\n" +
            "Usuario: " + self.cv.user + " \r\n" +
            "Total de estacionamientos: " + totStr + " \r\n" +
            "Promedio de estacionamientos libres: 50% \r\n" +
            "Salida: " + d2 + " " + t2 +"\r\n" +
            "--------------------\r\n\r\n"           
            )

        form.destroy()

        os.system("logIn_screen.py 1")


# clase que brega con todo lo que tenga que ver con el reporte
class ReportGen():

    fm = formManipulation()

    cv = checkValidation()

    #mainLog = ""

    #desplega el form del reporte y lee el file de reportes
    def repForm(self):

        #form
        form3 = Tk()

        form3.title("Parkaider")

        form3.geometry("500x500")

        with open(fm.d1 + ".txt") as f:

            out = f.read()


        repBox = LabelFrame(form3, text = "Reporte generado", height = 420, width = 260)

        repBox.place( x = 30, y = 10)

        scroll = Scrollbar(repBox)

        scroll.pack( side = RIGHT, fill = Y)

        txt = Text(repBox, text = None, yscrollcommand = scroll.set)

        txt.insert(END, out)

        txt.config( width = 50, height = 28)

        txt.pack()

        txt.config( state = DISABLED)

        scroll.config(command = txt.yview)

        #genera reporte cuando se actualiza el valor de estacionamientos
    def totUpdateLog(self):

        fm.callChange()

        with open(fm.d1 + ".txt", "a+") as f:

            f.write("--Total de parking actualizado-- \r\n\r\n"+

       "Total de estacionamientos : "+ fm.tot +"\r\n\r\n" +

        fm.d1 + " " + fm.t1 + "\n" +
        "--------------------------------\r\n\r\n")

    #llama al metodo de logout y genera el reporte final
    def finRep():

        fm.openLogIn()




#class PicProcessing:






#main form
fm = formManipulation()

rg = ReportGen()

global form

form = Tk()

form.title("Main Page")

form.geometry("600x700+100+0")

#box imagen capturada
#esta aqui por que sino sale undefined para lblImg
imgBox = LabelFrame(form, text = "Imagen capturada", height = "300", width = "260")

#path de foto
#esta aqui por que sino sale undefined para lblImg
img = PhotoImage(file = "parking.gif", height = "300", width = "260")

#foto
#es global para que la pueda reconocer la clase cuando corra killImg()
global lblImg

lblImg = Label(imgBox, image = img)

#big lbl PArkaider
lblTitleVal = StringVar()

lblTitleVal.set("Parkaider")

lblTitle = Label(form, textvariable = lblTitleVal, font=("Times", 30))

lblTitle.place( x = 220, y = 20)


#total de parking. Se convierte en string para usarse en el txtBox


strinTot = StringVar()

#conversion de totPark(int) a string
fm.tot = str(fm.tot)

#txtBox con el total de parking.
txtBoxPark = Entry(form, textvariable = None, width = 10)

txtBoxPark.insert(0,fm.tot)

txtBoxPark.place( x = 264, y = 135)

#lbl total de estacionamientos
lblParkingTotVal = StringVar()

lblParkingTotVal.set("Total de estacionamientos")

lblParkingTot = Label(form, textvariable = lblParkingTotVal, font = ("Times"))

lblParkingTot.place( x = 220, y = 100)

#btn edit,se usa si se fuera hacer update al algoritmo con el total nuevo de parking que no sea el default
btnEditTot = Button(form, text = "Actualizar", width = 8, command = rg.totUpdateLog)

btnEditTot.place( x = 263 , y = 170 )


#show el box de imagen capturada
imgBox.place(x = 170, y = 220)


#txtBox con la cantidad total despues que confirma que la imagen es correcta. Se supo que ahi se haga el algoritmo de object detection
txtBoxDisp = Entry(form, textvariable = None, width = 30)

txtBoxDisp.place( x = 210, y = 650)

#btn que captura una imagen con la camara
btnCap = Button(form, text = "Capturar" , width = 7, command = fm.displayImg)

btnCap.place( x = 270, y = 550)


#lbl disponibilidad
lblDisVal = StringVar()

lblDisVal.set("Disponibilidad")

lblDis = Label(form, textvariable = lblDisVal, font = "Times")

lblDis.place( x = 255, y = 620)


btnHelp = Button(form, text = "help", width = 4, command = fm.openHelp)

btnHelp.place(x = 20, y = 20)


btnLogout = Button(form, text = "Logout", width = 5, command = fm.openLogIn)

btnLogout.place(x = 540, y = 20)


btnRep = Button(form, text = "Reporte", width = 6, command = rg.repForm)

btnRep.place( x = 70, y = 20 )


#se necesita para que todo funcione en windows, en linux o mac, esto no hace falta
form.mainloop()

Ignore all of the Spanish comments and the Spanglish variables lol.

Thanks

2 Answers 2

1

You have to create a root window before you can create instances of StringVar or IntVar

Sign up to request clarification or add additional context in comments.

Comments

1

Put form = Tk() befor class declaration and before imported class

global form

form = Tk()

from logIn_screen import checkValidation

class formManipulation:
    # rest of code

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.