700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > python鼠标选中文本内容_【求助】使用Python脚本如何获取Windows中选中的文本

python鼠标选中文本内容_【求助】使用Python脚本如何获取Windows中选中的文本

时间:2018-07-20 00:36:16

相关推荐

python鼠标选中文本内容_【求助】使用Python脚本如何获取Windows中选中的文本

只能再用google搜了

发现一段话,也许可能大概差不多解释了不起作用的原因

环境变量表是保存在每个进程的用户空间的最高地址的,一个进程无权修改别的进程的用户空间内容

没办法了,只能出大招了。

最终,考虑到在规划的任务中第3个任务需要修改注册表值以添加右键菜单,于是想到也许环境变量也可以通过修改注册表来实现,简单、暴力

在网上搜索python操作注册表的模块,还真有。

python有一个内置模块_winreg可以用来操作注册表。

工具有了,下面来了解操作方法,操作之前得先了解环境变量在注册表中的位置、键值等等信息

搜索一通,找到了位置

对于用户环境变量,写入注册表的 HKEY_CURRENT_USER/Environment

对于系统环境变量,写入注册表的 HKEY_LOCAL_MACHINE/SYSTEM/ControlSet001/Control/SessionManager/Environment

ControlSet分几种情况

ControlSet:表示运行时的配置

ControlSet001:表示系统真实的配置信息

ControlSet002:表示最近一次成功的配置信息

少废话,上代码

version 1.2

# coding:utf-8

"""

使用注册表操作模块取出和设置环境变量

"""

import _winreg

newenv = 'c:\\python271;'

user_env = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,'Environment')

# print user_env

value,type = _winreg.QueryValueEx(user_env,'PATH')

# print value,type

tempenv = value + newenv

# print tempenv

try:

_winreg.SetValueEx(user_env,'PATH',0,type,tempenv)

except WindowsError:

print 'excetion'

finally:

_winreg.CloseKey(user_env)

执行后,SetValueEx一直抛异常,不知是什么原因

搜索一通,将OpenKey函数换成CreateKey函数后执行正常,PATH的值发生了变化

对比两个函数

_winreg.CreateKey(key, sub_key)

Creates or opens the specified key, returning a handle object.

key is an already open key, or one of the predefined HKEY_* constants.

sub_key is a string that names the key this method opens or creates.

If key is one of the predefined keys, sub_key may be None. In that case, the handle returned is the same key handle passed in to the function.

If the key already exists, this function opens the existing key.

The return value is the handle of the opened key. If the function fails, a WindowsError exception is raised.

_winreg.OpenKey(key, sub_key[, res[, sam]])

Opens the specified key, returning a handle object.

key is an already open key, or any one of the predefined HKEY_* constants.

sub_key is a string that identifies the sub_key to open.

res is a reserved integer, and must be zero. The default is zero.

sam is an integer that specifies an access mask that describes the desired security access for the key. Default is KEY_READ. See Access Rights for other allowed values.

The result is a new handle to the specified key.

If the function fails, WindowsError is raised.

网上说这个可能跟_winreg模块与系统之间的配合有关系

再优化一下version 1.2

version 1.2.1

# coding:utf-8

"""

使用注册表操作模块取出和设置环境变量

"""

import _winreg

envlist = []

newenv = 'c:\\python271;'

user_env = _winreg.CreateKey(_winreg.HKEY_CURRENT_USER,'Environment')

# print user_env

value,type = _winreg.QueryValueEx(user_env,'PATH')

# print value,type

envlist = value.split(';')

for env in envlist:

if env == newenv:

print 'exist'

exit()

tempenv = value + newenv + ';'

# print tempenv

try:

_winreg.SetValueEx(user_env,'PATH',0,type,tempenv)

except WindowsError:

print 'excetion'

finally:

_winreg.CloseKey(user_env)

至此,任务4(设置环境变量的脚本)算是有了影。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。