【发布时间】:2020-04-18 20:47:00
【问题描述】:
我需要将此路径变量从第一类导入另一个类,这是我的代码:
import openpyxl
class Excell():
global path
def send_message(self, data):
global path
print("Path Excel is : '{}'".format(data))
path = data # I need to export this variable 'path'
global path
class First():
global path
wb = openpyxl.load_workbook(path)
sheet = wb['sheet']
firstCell= sheet["A1"].value
print("Cell is :" + firstCell)
运行代码后,我看到这条消息:
C:\Python\python.exe E:/PycharmProjects/test/firstTest.py
Traceback (most recent call last):
File "E:\PycharmProjects\test\firstTest.py", line 11, in <module>
class First():
File "E:\PycharmProjects\test\firstTest.py", line 13, in First
wb = openpyxl.load_workbook(path)
NameError: name 'path' is not defined
Process finished with exit code 1
【问题讨论】:
-
恕我直言,您的代码没有任何意义!有很多问题:你为什么要使用类?这段代码应该做什么?你想达到什么目标?
-
一个问题是类
First在其类定义中使用path...这发生在任何调用Excell().send_message(data)之前。 -
@GrajdeanuAlex。函数 send_message(self, data) ,从 GUI 模块调用 excel 路径。看这篇文章raspberrypi.org/forums/viewtopic.php?t=195973,不过我是用excel浏览Excel的。
-
@tdelaney 不工作
标签: python python-3.x selenium tkinter