从另一个脚本调用类变量而不再次初始化它们的正确方法

时间:2014-02-27 01:10:35

标签: python tkinter

我试图通过将调用导入新脚本来从另一个脚本调用这三个类变量。这就是它的工作方式,当logIn.py运行时如果输入正确的用户名和密码,它们将保存在用户和密码变量中。如果失败,则将1添加到失败变量中。在第一天,我有这样的fail = 0, user = "", password = ""因为我得到了未定义的错误。一切都有效,但当我创建该类的对象时,所有内容都再次初始化为fail = 0, user = "", password = ""。所以我想“也许我可以使用StringVarIntVar来解决问题”。但相反,我得到了这个错误。

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'

我很确定它与String,IntVar有关。如果这样做正常,在mainPage3上我将生成一个报告,这3个变量是我将要使用的变量。

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(我从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()

忽略所有西班牙语评论和Spanglish变量lol。

由于

2 个答案:

答案 0 :(得分:1)

您必须创建根窗口才能创建StringVarIntVar

的实例

答案 1 :(得分:1)

在类声明之前和导入的类

之前放置form = Tk()
global form

form = Tk()

from logIn_screen import checkValidation

class formManipulation:
    # rest of code
相关问题