【问题标题】:How can import variable beteen classes in same module如何在同一模块中的类之间导入变量
【发布时间】: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


【解决方案1】:

删除globals。

有发送函数return路径值。

就在First类之前,给一个新定义的全局变量赋值:

path = Excell().send_message("foo")

然后,当您分配给工作簿时,该值可用。

【讨论】:

    【解决方案2】:

    我使用这个但不工作我看到这条消息我导入函数`def send_message(self,data):

    来自 GUI 模块,例如此示例 https://www.raspberrypi.org/forums/viewtopic.php?t=195973,但将更改条目更改为标签从浏览文件获取路径:

    import openpyxl
    
    class Excell():
    
         def send_message(self, data):
    
               print("Path Excel is : '{}'".format(data))
               return data
    
    path = Excell().send_message("foo")
    
    class First():
    
           wb = openpyxl.load_workbook(path)
           sheet = wb['sheet']
           firstCell = sheet["A1"].value
           print("Cell is :" + firstCell)
    

    我看到这个错误:

    C:\Python\python.exe E:/PycharmProjects/Amazon/bb.py
    Path Excel is : 'data'
    Traceback (most recent call last):
          File "E:/PycharmProjects/Amazon/bb.py", line 12, in <module>
               class First():
          File "E:/PycharmProjects/Amazon/bb.py", line 14, in First
               wb = openpyxl.load_workbook(path)
          File "C:\Python\lib\site-packages\openpyxl\reader\excel.py", line 312, in                load_workbook
                reader = ExcelReader(filename, read_only, keep_vba,
          File "C:\Python\lib\site-packages\openpyxl\reader\excel.py", line 124, in __init__
                self.archive = _validate_archive(fn)
          File "C:\Python\lib\site-packages\openpyxl\reader\excel.py", line 94, in _validate_archive
                raise InvalidFileException(msg)
    openpyxl.utils.exceptions.InvalidFileException: openpyxl does not support  file format, please check you can open it with Excel first. Supported formats are: .xlsx,.xlsm,.xltx,.xltm
    

    进程以退出代码 1 结束

    【讨论】:

    • 您实际上应该提供 excel 文件的路径,而不是 send_message(“data”)。比如:send_message(“C:\path\excel_file.xlsx”)
    • 这条路径是动态的。从 GUI tkinter 上传后,我会自动获取路径。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多