繁体   English   中英

导入错误python tkinter

[英]Importing error 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

这是我要导入的脚本:

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次直到正确为止。 我为西班牙语表示歉意,只是忽略所有评论

您正在尝试从LogInScreen.py文件中的class中导入方法。 方法getUsergetPass是属于类checkValidation bound methods ,您需要导入class并对class进行调用

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”中的代码括起来,以检查由解释器评估的文件是否为正在运行的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名称,即file_to_import

更改为此:

from logInScreen import checkValidation

然后,使用:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM