5

请耐心等待,因为我对编程还很陌生。我的基本问题是这个。我有一个用 Haskell 编写的程序,我想将它的标准输出连接到 Python 程序的标准输入(它将管理与 GUI 相关的东西)。同样,我想将 Python 程序的标准输出连接到 Haskell 程序的标准输入,以便它可以将有关用户单击/键入的内容的信息发送到 Haskell 程序。

第一个问题是,如果我在两者之间建立一个管道,假设 Python 程序的标准输出连接到 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.

很抱歉,如果之前已经问过这个问题,但感谢您帮助我!

4

2 回答 2

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()                                                                                                           
于 2013-03-18T09:54:43.713 回答
-2

您还可以从命令 shell 建立管道:

我的python.py | myhaskell.hs

Haskell 程序将像对任何其他类型的标准输入一样响应,如下所示:

myhaskell.hs

于 2013-03-18T04:02:51.997 回答