简体   繁体   English

如何在命令提示符下使用“ SetText”?

[英]How can I use 'SetText' in command prompt?

[1] The code is: [1]代码为:

import pywinauto
app = pywinauto.Application()
mainApplication = app.window_(title_re = ".*Environment.*")
mainApplication.Minimize()
mainApplication.Edit.SetText("test", with_spaces=True)
mainApplication.Edit.SetText("{ENTER}")

[2] The output is: [2]输出为:

  File "C:\Python27\lib\site-packages\pywinauto\application.py", line 239, in __getattr__
    ctrls = _resolve_control(self.criteria)
  File "C:\Python27\lib\site-packages\pywinauto\application.py", line 754, in _resolve_control
    raise e.original_exception
WindowNotFoundError

Note: If I use 'TypeKeys' method I do not encounter any problem, but I want to write in an app. 注意:如果使用'TypeKeys'方法,我不会遇到任何问题,但是我想在应用程序中编写。 even if focus is not set on it. 即使未将重点放在上面。

Unfortunately cmd.exe has no any controls inside the window. 不幸的是,cmd.exe在窗口内没有任何控件。 So you cannot send WM_SETTEXT message to it. 因此,您无法向其发送WM_SETTEXT消息。 That's why SetText isn't working. 这就是SetText无法正常工作的原因。 And it will never work for cmd.exe because GUI automation tool is for GUI, not for command line. 它永远不会对cmd.exe起作用,因为GUI自动化工具是针对GUI而不是命令行。

Generally you can interact with cmd.exe process through pipes using standard module subprocess (class Popen ). 通常,您可以使用标准模块subprocess进程(类Popen )通过管道与cmd.exe进程进行交互。

You can try the following: 您可以尝试以下方法:

  1. run cmd.exe through Popen , 通过Popen运行cmd.exe
  2. Connect(path='cmd.exe') using pywinauto, 使用pywinauto Connect(path='cmd.exe')
  3. call app.Window_().Minimize() , 调用app.Window_().Minimize()
  4. send keys using p.stdin.write(someInput) where p is a Popen object. 发送用密钥p.stdin.write(someInput)其中p是一个Popen对象。

When you're using pipes, there are some pitfalls with potential dead lock of input and output streams. 当使用管道时,输入流和输出流可能会陷入死锁。 Here is one method to workaround some issues: Running an interactive command from within python 这是解决某些问题的一种方法: 从python内部运行交互式命令

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Python在Windows命令提示符下使用颜色? - How do I use colour with Windows command prompt using Python? 如何通过命令提示符导入python文件? - How can I import a python file through a command prompt? 如何使用 Python 访问命令提示符历史记录 - How can I access command prompt history with Python 如何从命令提示符运行 Pygame Zero 程序? - How can I run a Pygame Zero program from the Command Prompt? 如何在命令提示符下将 python 变量传递给进程 - How can I pass a python variable to a process in the command prompt 如何在命令提示符中运行 VSCode 中的文件 - How can I run a file in VSCode in the Command Prompt 即使可以使用echo命令打印其值,也无法在命令提示符下使用环境变量 - Unable to use an environment variable in command prompt, even though I can print it's value using echo command 如何在 Miniforge(不是 Miniconda)中访问/安装 Powershell 提示符或等效命令提示符? - How can I access/install a Powershell prompt or equivalent command prompt within Miniforge (not Miniconda)? 如何使用 Selenium/Webdriver 提示输入并使用结果? - How can I prompt for input using Selenium/Webdriver and use the result? 如何在 windows 命令提示符上使用 python 模块 - How to use python module on windows command prompt
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM