python调用脚本产生的变量放入主脚本_如何在导入的模块/脚本中引用主脚本中的变量?...

In my imported script 'scan.py' I want to refer to a value of a variable created and initialized before in the main script. Here's the code from the main script:Input = EJ.get()

os.system ('python scan.py')

FileName = str(r"C:\\Users\\Admin\\Documents\\HPython\\WESMFG\\POnumber\\" + Input + ".pdf")

and the relevant code of the scan.py script:

output_file = Input

print("Output file: %s" % output_file)

.

.

.

print("Writing output file ...")

img = scan_session.images[0]

img.save(output_file, "JPEG")

print("Done")

'Input' stores the Input of the Entry widget EJ from my Tkinter GUI. So I want the user to type in something, so this input gets the name of the new scanned document.

When running the main script, it tells me 'Input' is not defined.

Since I'm kind of new to programming on python, maybe I'm falsely ignoring

if __name__ == "__main__":

pyinsane2.init()

try:

main()

finally:

pyinsane2.exit() at the end of scan.py. Does this matter in any way to my question?

What I have tried:

import __main__

main_global1= __main__.global1 but since 'Input' is in a function it tells me there is no variable in main called 'Input'

解决方案You cannot use a local variable in a program that is running in its own environment. You need to pass the value as a parameter on your system call; see 29.1. sys — System-specific parameters and functions — Python 3.6.4 documentation[^]

Your code should be:

Input = EJ.get()

#do not use 'r' prefix if the string uses escape sequences

FileName = str("C:\\Users\\Admin\\Documents\\HPython\\WESMFG\\POnumber\\" + Input + ".pdf")

os.system ('python scan.py ' + FileName)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值