[英]Importing error python tkinter
I've been trying to run this simple test just to get the feel of how to import methods from another script, but I have two problems: 我一直试图运行这个简单的测试,只是为了感觉如何从另一个脚本导入方法,但是我有两个问题:
the whole program runs instead of just importing and calling, the methods, when I need them too. 整个程序运行,而不仅仅是在我需要它们时才导入和调用方法。
I'm getting this error: 我收到此错误:
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: cannot import name getUser ImportError:无法导入名称getUser
This is the script I'm importing to: 这是我要导入的脚本:
from logInScreen import getUser, getPass
class hello:
def show(self):
usr = getUser()
ps = getPass()
str(usr)
str(ps)
h = hello()
h.show()
This is what's on logInScreen.py : 这是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()
I hope I got the indentation right, kinda off a pain going through every line and spacing 4 times till it's right. 我希望我的压痕正确无误,有点痛苦,它经过每行并间隔4次直到正确为止。 I apologize for the Spanish, just ignore all the comments lol
我为西班牙语表示歉意,只是忽略所有评论
You are attempting to import methods from within a class
in your LogInScreen.py
file. 您正在尝试从
LogInScreen.py
文件中的class
中导入方法。 The methods getUser
and getPass
are bound methods
that belong to the class checkValidation
, You need to instead import the class
and make the call on your class instead 方法
getUser
和getPass
是属于类checkValidation
bound methods
,您需要导入class
并对class
进行调用
from LogInScreen import checkValidation
validation = checkValidation()
validation.getUser()
validation.getPass()
As a simple illustration consider the following example involving two files: 作为简单说明,请考虑以下涉及两个文件的示例:
file_to_import_from.py
(Analogous to your logInScreen.py
) 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
(Analogous to the script you are importing to) 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()
And see Output 并查看输出
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
As you can see, the first two calls worked, but the second time the error you are facing arose, this is because the method my_class_bound_method
exists as a method within the class MyClass
. 如您所见,前两个调用有效,但是第二次出现您遇到的错误,这是因为
my_class_bound_method
方法作为类MyClass
的方法存在。
EDIT : 编辑 :
In response to your comment, to avoid running the whole file, surround the code in your 'LogInScreen.py' in an if
statement which checks if the file being evaluated by the interpreter is the main
file being run. 为了回应您的评论,为避免运行整个文件,请在
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()
The if
statement I added here checks a special environment variable that python
creates when it interprets a file, which is __name__
, when the file is the file that you are directly running: python file_i_am_running.py
the variable __name__
is set the the string "__main__"
to indicate it is the main method, when it is not the file being run, and maybe one being imported as in your case, the variable is set to the module
name, ie file_to_import
. 我在此处添加的
if
语句检查python
在解释文件时创建的特殊环境变量__name__
,而该文件是您直接运行的文件时: python file_i_am_running.py
变量__name__
设置为字符串"__main__"
表示它是主要方法,当它不是正在运行的文件时(可能是一种导入的情况),则将变量设置为module
名称,即file_to_import
。
Change to this: 更改为此:
from logInScreen import checkValidation
And then, use: 然后,使用:
check = checkValidation()
usr = check.getUser()
ps = check.getPass()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.