我有两个不同的.py文件,可以在另一个文件中使用其中一个的变量吗?你知道吗
对不起,我的错误代码,但我仍在学习:)
在主文件.py你知道吗
from LoginFile import login
class MyApp(QtGui.QDialog, Ui_login_dialog):
def __init__(self, parent=None):
super(MyApp,self).__init__()
self.setupUi(self)
self.login_btn.clicked.connect(self.loginCheck)
self.Register.clicked.connect(self.registerCheck)
self.window2 = None
total_usernames = c.execute("SELECT USERNAME FROM register_table").fetchall()
for data in total_usernames:
self.comboBox.addItems(data)
def login_check(self):
username = str(self.comboBox.currentText())
password = str(self.fieldpassword.text())
login(username, password)
# I can't read the following variables from the other file
print(current_status)
print(current_username)
在登录文件.py我有:
def login(username, password):
driver=webdriver.Chrome(r"C:\Python27\selenium\webdriver\chromedriver.exe")
driver.get(site)
current_username = driver.find_element_by_xpath(".//*[@id='react-root']/section/main/article/header/div[2]/ul/li[1]/span/span").text
current_status = driver.find_element_by_xpath(".//*[@id='react-root']/section/main/article/header/div[2]/ul/li[2]/a/span").text
基本上我想阅读“current\u status”和“current\u username”变量主文件.py进口自登录文件.py你知道吗
我已经尝试从其他文件导入所有内容,包括:
from LoginFile import *
但它不起作用。我也试着检查类似问题的答案,但我没能找到一个适合我的解决方案。你知道吗
有可能吗?有没有更好的解决办法来达到同样的结果?你知道吗
提前多谢!!!你知道吗
将目录定义为python目录/打包目录结构
from LoginFile import *
允许您访问LoginFile类/模块中编写的所有函数,而不是其变量。访问该类中变量的常见解决方法是使用另一个函数,如getVariable(),它只返回感兴趣的变量。然后在主文件中通过LoginFile.getVariable文件(). 你知道吗您尝试导入的变量位于函数内部,因此它们超出了范围。即使您调用了函数,它们也会在函数结束时被销毁,除非您返回他们。那个另一种方法是将它们和其他逻辑放在函数之外,然后在导入时执行逻辑并分配变量,然后您就可以访问它们的值。所有这些都是在这样的假设下进行的,即当解释器到达您导入的行时,您没有收到任何错误。你知道吗
相关问题 更多 >
编程相关推荐