我的问题是这样的:我正在尝试使用Python接口作为程序(已经使用FORTRAN制作),以便用户可以通过打开程序并发送输入来引入输入,但它从不适用于某些程序原因。
我导入以下内容:
import simpleguitk,os
from subprocess import Popen,PIPE,STDOUT
我已经在这里找到了不同的答案,我写了以下内容:
p = Popen(['potscat2.exe'],stdin=PIPE)#,stdout=PIPE,stderr=STDOUT)
p.communicate(str(opmenu[0])+"\n"+str(opmenu[1])+"\n"+str(opmenu[2])+"\n"+str(opmenu[3][0]*pow(10,opmenu[3][1]))+"\n","UTF-8")
或者,有以下内容:
p = Popen(['potscat2.exe'],stdin=PIPE)#,stdout=PIPE,stderr=STDOUT)
p.stdin.write(str(opmenu[0]))
p.stdin.write(str(opmenu[1]))
p.stdin.write(str(opmenu[2]))
p.stdin.write(str(opmenu[3][0]*pow(10,opmenu[3][1])))
但这两种选择都不起作用。
前者给出了这个错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
return self.func(*args)
File "C:\Python34\lib\site-packages\simpleguitk\input.py", line 90, in _mouse_click
self._mouse_click_handler(pos)
File "C:\Users\Miguel\PythonWorkplace\PotScat\PotScat2.py", line 357, in mouse_control
b.mouse_act(position)
File "C:\Users\Miguel\PythonWorkplace\PotScat\PotScat2.py", line 106, in mouse_act
self.action()
File "C:\Users\Miguel\PythonWorkplace\PotScat\PotScat2.py", line 112, in <lambda>
butF = Button((1050,800),(120,50),"Finish", lambda : finish())
File "C:\Users\Miguel\PythonWorkplace\PotScat\PotScat2.py", line 280, in finish
p.communicate(str(opmenu[0])+"\n"+str(opmenu[1])+"\n"+str(opmenu[2])+"\n"+str(opmenu[3][0]*pow(10,opmenu[3][1]))+"\n","UTF-8")
File "C:\Python34\lib\subprocess.py", line 954, in communicate
endtime = _time() + timeout
TypeError: unsupported operand type(s) for +: 'float' and 'str'
后者给出了这个错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
return self.func(*args)
File "C:\Python34\lib\site-packages\simpleguitk\input.py", line 90, in _mouse_click
self._mouse_click_handler(pos)
File "C:\Users\Miguel\PythonWorkplace\PotScat\PotScat2.py", line 357, in mouse_control
b.mouse_act(position)
File "C:\Users\Miguel\PythonWorkplace\PotScat\PotScat2.py", line 106, in mouse_act
self.action()
File "C:\Users\Miguel\PythonWorkplace\PotScat\PotScat2.py", line 112, in <lambda>
butF = Button((1050,800),(120,50),"Finish", lambda : finish())
File "C:\Users\Miguel\PythonWorkplace\PotScat\PotScat2.py", line 281, in finish
p.stdin.write(str(opmenu[0]))
TypeError: 'str' does not support the buffer interface
有人能帮帮我吗?您知道,我在Windows 7上使用 Python 3.4 。可执行文件必须逐个接收数据,但它们彼此独立。
帮助?
编辑:采取&#34; UTF-8&#34;从第一个替代品中取出我提到的TypeError(它将它计为超时),但它在交换中给出了与另一种可能性相同的TypeError。
opmenu 定义为
opmenu = [0,0,0,[0,0]]
所有值都是整数,除 opmenu [3] [0] 外,这是一个 float 。
答案 0 :(得分:0)
p.communicate(str(opmenu[0])+"\n"+str(opmenu[1])+"\n"+str(opmenu[2])+"\n"+str(opmenu[3][0]*pow(10,opmenu[3][1]))+"\n","UTF-8")
communicate
的第二个参数是timeout
。你给它“UTF-8”,但这不是一个有效的值。 timeout
应为数字或None
。
尝试使用一个参数调用communicate
。
p.communicate(str(opmenu[0])+"\n"+str(opmenu[1])+"\n"+str(opmenu[2])+"\n"+str(opmenu[3][0]*pow(10,opmenu[3][1]))+"\n")