请耐心等待我,因为我对编程很新。我的基本问题是这个。我有一个用Haskell编写的程序,我的stdout要连接到Python程序的stdin(它将管理GUI相关的东西)。类似地,我想将Python程序的stdout连接到Haskell程序的stdin,以便它可以发送有关用户单击/输入到Haskell程序的信息。
第一个问题是,如果我在两者之间建立一个管道,假设Python程序的stdout连接到Haskell程序,如果我使用Tkinter创建小部件和东西,它们是否会显示在屏幕呢?
第二个问题是我究竟如何建立这条管道?请考虑以下示例代码..
main :: IO ()
main = do
-- putStrLn "Enter a number." <- this will be displayed in Python
string <- getLine
putStrLn $ 5 + read string::Int -- or any equivalent function to send to stdout
Python代码看起来像这样。
from Tkinter import *
root = Tk()
label = Label(root, text = "Enter a number.")
label.pack()
enternum = Entry(root)
enternum.pack()
enternum.bind("<Return>", print_num)
-- print_num would essentially be a function to send the Haskell program the number
-- which would be received by the getLine function the way I have it.
如果之前已经提出过这个问题,我很抱歉,但感谢您帮助我!
答案 0 :(得分:1)
我是使用Twisted完成的,因为它提供了对轮询的很好的抽象。基本上你需要首先定义Python和Haskell程序如何相互通信的方式(称为Twisted中的协议),例如,数据包的持续时间,如何处理错误等等。然后你只需要对它们进行编码。
这是haskell代码:
-- File "./Hs.hs"
import Control.Concurrent
import System.IO
main = do
-- Important
hSetBuffering stdout NoBuffering
-- Read a line
line <- getLine
-- parse the line and add one and print it back
putStrLn (show (read line + 1))
-- Emphasize the importance of hSetBuffering :P
threadDelay 10000000
这是Python代码:
# File "./pyrun.py"
import os
here = os.path.dirname(os.path.abspath(__file__))
from twisted.internet import tksupport, reactor, protocol
from twisted.protocols.basic import LineReceiver
from Tkinter import Tk, Label, Entry, StringVar
# Protocol to handle the actual communication
class HsProtocol(protocol.ProcessProtocol):
def __init__(self, text):
self.text = text
def connectionMade(self):
# When haskell prog is opened
towrite = self.text + '\n'
# Write a line to the haskell side
self.transport.write(towrite)
def outReceived(self, data):
# When haskell prog write something to the stdout
# Change the label in the tk window to be the received data
label_var.set(data[:-1])
def send_num_to_hs(_event):
content = enternum.get()
# The abspath of the haskell program
prog = os.path.join(here, 'Hs')
reactor.spawnProcess(HsProtocol(content), # communication protocol to use
prog, # path
[prog] # args to the prog
)
# Setting up tk
root = Tk()
# On main window close, stop the tk reactor
root.protocol('WM_DELETE_WINDOW', reactor.stop)
# Since I'm going to change that label..
label_var = StringVar(root, 'Enter a number')
# Label whose content will be changed
label = Label(root, textvariable=label_var)
label.pack()
# Input box
enternum = Entry(root)
enternum.pack()
enternum.bind('<Return>', send_num_to_hs)
# Patch the twisted reactor
tksupport.install(root)
# Start tk's (and twisted's) mainloop
reactor.run()
答案 1 :(得分:-2)
您还可以从命令shell建立管道:
mypython.py | myhaskell.hs
Haskell程序将尽可能地响应 任何其他类型的标准输入,如:
myhaskell.hs