简体   繁体   English

将子流程调用的输出打印到标签tkinter中

[英]print output of subprocess call into a label tkinter

I am trying to create a GUI in tkinter. 我正在尝试在tkinter中创建一个GUI。 The Gui is basically for Mcp23017 . 该Gui基本上是针对Mcp23017的 I am trying to configure the input and output pins so that the user could change them according to their choice.. There is also an option to make the inputs/outputs high or low.. 我正在尝试配置输入和输出引脚,以便用户可以根据自己的选择进行更改。.还有一个使输入/输出为高或低的选项。

Now I am trying to read a pin using 'i2cget'(using a seperate function).. I need to display the output of this subprocess call into a label on the gui.. 现在,我尝试使用“ i2cget”(使用单独的函数)读取一个引脚。.我需要将此子进程调用的输出显示到gui上的标签中。

This is what my code looks like: 这是我的代码如下所示:

def read_p22():
   output = subprocess.call(['i2cget -y 0x20 0x12 0xFD'],shell=True)
   x=print (output)
   Label(tableframe,textvariable=x).grid(row=2,column=20)
   root.after(5000, read_p22)

When this function excutes(by pressing a button), however it prints a value '1' on the python shell alternatly when I press the button... I dont know how to redirect the output to the label.. Could somebody suggest something? 当此函数执行(通过按下按钮)时,但是当我按下按钮时,它会交替在python外壳上打印一个值'1'...我不知道如何将输出重定向到标签。有人可以建议吗?

Update:: On executing the suggested commands: 更新:在执行建议的命令时:

process = subprocess.Popen(['i2cget -y 0x20 0x12 0xFD'], stdout=PIPE,      stderr=PIPE, shell=True)
output, code = process.communicate()

I printed the 'output' and 'process' and they gave me the follwing respectivley: 我打印了“输出”和“过程”,它们给了我以下的尊重:

b'' <subprocess.Popen object at 0x00000000035CB2B0>

Since nothing is connected to the pin I expect it to return a value of '0'.. I don't know what is the b'' it is giving me ... 由于没有连接到该引脚,所以我希望它返回值“ 0”。我不知道它给我的b''是什么...

Any advice is really appreciated.. 任何建议都非常感激..

Kind Regards, Namita. 亲切的问候,南田。

What you have is the return code, not the output. 您拥有的是返回码,而不是输出。

From subprocess.call docs: subprocess.call文档:

Run the command described by args. 运行args描述的命令。 Wait for command to complete, then return the returncode attribute . 等待命令完成,然后返回returncode属性

Instead, use subprocess.Popen to open a process, subprocess.PIPE to pipe the output to you, and Popen.communicate get the output: 相反,使用subprocess.Popen打开一个过程, subprocess.PIPE到管道输出到你, Popen.communicate得到的输出:

process = subprocess.Popen(['i2cget -y 0x20 0x12 0xFD'], stdout=PIPE, stderr=PIPE, shell=True)
output, code = process.communicate()
# do stuff with output...

There are multiple things wrong in your code. 您的代码中有很多错误。 You should split your problem into tiny tiny subtasks and complete each task independently. 您应该将问题分解成很小的子任务,然后独立完成每个任务。

1. Get output from a subprocess as a string variable in Python 1.从子流程中获取输出作为Python中的字符串变量

subprocess.call() returns an integer (exit status), not string (the output) -- start Python REPL and try to run any command via subprocess and see for yourself: it should help you to understand what is happening in addition to reading the linked documentation. subprocess.call()返回一个整数(退出状态),而不是字符串(输出)-启动Python REPL并尝试通过subprocess运行任何命令,亲自看看:除了阅读外,它还可以帮助您了解正在发生的事情链接的文档。

You could use subprocess.check_output() , to get the output if you don't mind blocking your GUI thread -- nothing will respond until the subprocess returns. 如果您不介意阻塞GUI线程,则可以使用subprocess.check_output()获取输出-在子进程返回之前,没有任何响应。

Otherwise (if i2cget does not return in an instant, always), you could do it asynchroniously using threads or .createfilehandler() method . 否则(如果i2cget不会立即返回),则可以使用线程或.createfilehandler()方法异步进行

2. Learn why x = print(output) is useless in Python 2.了解为什么x = print(output)在Python中无用

It is SyntaxError in Python 2 by default. 默认情况下,它是Python 2中的SyntaxError。 Otherwise print() function always returns None ie, you could have written x = None (except that output is printed to stdout as a side-effect). 否则, print()函数将始终返回None即,您可能已将x = None写入(除非将output作为副作用打印到stdout)。

3. Update text in a Label 3.更新标签中的文本

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM