导入错误python tkinter

时间:2014-02-25 21:00:45

标签: python tkinter

我一直在尝试运行这个简单的测试,以了解如何从另一个脚本导入方法,但我有两个问题:

  1. 整个程序运行时,而不仅仅是导入和调用方法,当我需要它们时。

  2. 我收到了这个错误:

    Traceback (most recent call last):
      File "C:\Users\devilboy 4\Documents\Visual Studio 2013\Projects\classvariablesOnANewScript\classvariablesOnANewScript\classvariablesOnANewScript.py", line 1, in <module>
    from logInScreen import getUser, getPass
    

    ImportError:无法导入名称getUser

  3. 这是我要导入的脚本:

    from logInScreen import getUser, getPass
    class hello:
    
        def show(self):
            usr = getUser()
            ps = getPass()
            str(usr)
            str(ps)
    
    h = hello()
    h.show()
    

    这就是logInScreen.py上的内容:

    from tkinter import *
    import os
    import tkinter.messagebox
    
    #check si lo entrado es correcto
    class checkValidation:
        fail = 0
        user = "hi user"
        password = "nice pass"
    
       #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
    
    
    
    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
    passIn = Entry(form, 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()
    

    我希望我的缩进是正确的,有点痛苦地经过每一行并间隔4次直到它是正确的。我为西班牙语道歉,只是忽略所有评论lol

2 个答案:

答案 0 :(得分:4)

您正试图从class文件的LogInScreen.py内导入方法。方法getUsergetPass属于bound methods,属于checkValidation,您需要导入class并在您的班级进行调用< / p>

from LogInScreen import checkValidation

validation = checkValidation()
validation.getUser()
validation.getPass()

作为一个简单的说明,请考虑以下涉及两个文件的示例:

file_to_import_from.py(类似于您的logInScreen.py

class MyClass:
    def my_class_bound_method(self):
        print "hello I a method belonging to MyClass"

def my_method_not_belonging_to_a_class():
    print "Hello, I am a method that does not belong to a class"

file_to_import_to.py(类似于您要导入的脚本)

from file_to_import_from import my_method_not_belonging_to_a_class

my_method_not_belonging_to_a_class()


from file_to_import_from import MyClass

x = MyClass()
x.my_class_bound_method()

from file_to_import_from import my_class_bound_method

my_class_bound_method()

并参见输出

Hello, I am a method that does not belong to a class
hello I a method belonging to MyClass

Traceback (most recent call last):
  File "C:\Users\Joe\Desktop\Python\import_test2.py", line 10, in <module>
    from file_to_import_from import my_class_bound_method
ImportError: cannot import name my_class_bound_method

正如您所看到的,前两个调用有效,但第二次出现错误,这是因为方法my_class_bound_method作为类MyClass中的方法存在。

修改

为响应您的评论,为了避免运行整个文件,请在if语句中将“LogInScreen.py”中的代码括起来,检查解释器正在评估的文件是否为{{1}正在运行的文件。

main

我在这里添加的from tkinter import * import os import tkinter.messagebox #check si lo entrado es correcto class checkValidation: # CODE FROM YOUR CLASS OMITTED FOR BREVITY SAKE # NOTHING BEYOUND THIS IF STATEMENT WILL RUN WHEN THE FILE IS IMPORTED if __name__ == "__main__": vali = checkValidation() # MORE CODE OMITTED FOR BREVITY form.mainloop() 语句检查if在解释文件时创建的特殊环境变量python,当文件是您直接运行的文件时:__name__变量python file_i_am_running.py设置为字符串__name__以指示它是主要方法,当它不是正在运行的文件时,可能正在导入一个,如您的情况,变量设置为"__main__"名称,即module

答案 1 :(得分:0)

更改为:

from logInScreen import checkValidation

然后,使用:

check = checkValidation()
usr = check.getUser()
ps = check.getPass()