如何在 haskell/gtk2hs 中制作图形命令行?

发布于 2024-10-04 07:21:48 字数 368 浏览 6 评论 0原文

我正在尝试在 haskell 中创建我的第一个“真正的程序”(如果多项式可以解决积分问题),但我完全被这部分难住了:

我想做一些非常简单的东西,有点像 GHCi:

> user input
program output
> user input
program output
> user input
program output
> 

除了我的程序输出是图像(使用 LaTeX 将数学表达式转换为 PNG) - 所以我不能使用 System.IO 来做到这一点。我认为使用我已成功安装的 gtk2hs 是可能的,但我不知道如何进行此输入/输出对话。

如果您有时间,请向我展示它是如何完成的。多谢!

I'm trying to create my first "real program" in haskell (something that solves integrals if polynomials) but I'm completely stumped with this part of it:

I want to make something very simple a bit like GHCi:

> user input
program output
> user input
program output
> user input
program output
> 

except that my program output is images (using LaTeX to turn mathematical expressions into PNGs) - so I can't do this using System.IO. I think it will be possible with gtk2hs which I've managed to install but I can't figure out how to make this input/output dialogue.

Please show me how it's done if you have the time. Thanks a lot!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吻风 2024-10-11 07:21:48

感谢 ClaudiusMaximus,我们设法提出了以下解决方案。

module Main where

import Graphics.UI.Gtk

main = do
 initGUI

 ----------------

 win <- windowNew
 onDestroy win mainQuit

 vb <- vBoxNew False 3
 log <- vBoxNew False 2

 sc <- scrolledWindowNew Nothing Nothing
 scrolledWindowSetPolicy sc PolicyNever PolicyAutomatic

 sw <- layoutNew Nothing Nothing

 en <- entryNew

 ----------------

 scrolledWindowAddWithViewport sc log
 boxPackStart vb sc PackGrow 0
 boxPackStart vb en PackNatural 0
 set win [ containerChild := vb ]

 en `onEntryActivate` do
   txt <- entryGetText en
   entrySetText en ""
   l <- labelNew (Just txt)
   boxPackStart log l PackNatural 0
   widgetShowAll log
   Just ran <- scrolledWindowGetVScrollbar sc
   adj <- rangeGetAdjustment ran
   max <- adjustmentGetUpper adj
   adjustmentSetValue adj max

 ----------------

 widgetShowAll win
 mainGUI

We managed to come up with the following solution, thanks to ClaudiusMaximus.

module Main where

import Graphics.UI.Gtk

main = do
 initGUI

 ----------------

 win <- windowNew
 onDestroy win mainQuit

 vb <- vBoxNew False 3
 log <- vBoxNew False 2

 sc <- scrolledWindowNew Nothing Nothing
 scrolledWindowSetPolicy sc PolicyNever PolicyAutomatic

 sw <- layoutNew Nothing Nothing

 en <- entryNew

 ----------------

 scrolledWindowAddWithViewport sc log
 boxPackStart vb sc PackGrow 0
 boxPackStart vb en PackNatural 0
 set win [ containerChild := vb ]

 en `onEntryActivate` do
   txt <- entryGetText en
   entrySetText en ""
   l <- labelNew (Just txt)
   boxPackStart log l PackNatural 0
   widgetShowAll log
   Just ran <- scrolledWindowGetVScrollbar sc
   adj <- rangeGetAdjustment ran
   max <- adjustmentGetUpper adj
   adjustmentSetValue adj max

 ----------------

 widgetShowAll win
 mainGUI
旧情别恋 2024-10-11 07:21:48

我认为你应该首先实现后端,即解析命令和创建输出图像的代码。如果可行,您就可以实现 GUI。基本上,您需要一些轻量级的文本输入用于输入,文本和绘图小部件用于输出。由于 GUI 编程(恕我直言)并不简单,因此您应该首先查看一些 GTK/gtk2hs 教程/介绍。

I think you should first implement the backend, i.e. the code for parsing a command and creating the output image. If that works, you could then implement the GUI. Basically, you need something light a text entry for input and an text and drawing widget for output. As GUI programming is (IMHO) not trivial, you should first look at some GTK/gtk2hs tutorials / introductions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文