5. λ 初めてのWindow
λλ
import Graphics.UI.WX
main = start mainFrame
mainFrame = do
f <- frameFixed [text := quot;はろー Worldquot;]
p <- panel f []
set f [layout := minsize (sz 300 300) $ widget p]
● いきなりdo
– GUIを扱う命令はIOなので仕方がないです
● := でプロパティを設定可能
6. λ 初めてのWindow2
λλ
import Graphics.UI.WX
main = start mainFrame
mainFrame = do
s <- readFile “title.txt”
f <- frameFixed [text := utf8ToUcs4 s]
p <- panel f []
set f [layout := minsize (sz 300 300) $ widget p]
● 外部データをGUIに表示する時は変換関数を通す
7. λ はじめてのレイアウト
λλ
mainFrame = do
f <- frame [text := quot;titlequot;]
p <- panel f []
b <- button p [text := quot;pushquot;]
i <- textEntry p[text := quot;aquot;]
set p [layout := (row 2 [fill$widget i,widget b])]
set f [layout := (fill $ widget p)]
● button,textEntry
● widget,row,column,fill,等でレイアウトを整える
– リサイズに耐える設計
● 親をちゃんと指定しましょう
8. λ もっとレイアウト
λλ
mainFrame = do
f <- frame [text := quot;titlequot;]
p <- panel f []
b <- button p [text := quot;pushquot;]
i <- textEntry p[text := quot;aquot;]
l <- singleListBox p [items := [quot;inaquot;,quot;jobquot;,quot;kpcquot;]];
set p [layout :=(column 2 [
(row 2 [fill$widget i,widget b]),
fill$widget l
])]
set f [layout := (fill $ widget p)]
● singleListBox
9. λ イベント
λλ
mainFrame = do
f <- frame [text := quot;titlequot;]
p <- panel f []
b <- button p [text := quot;pushquot;,on command ::= buttonEv]
i <- textEntry p [text := quot;aquot;]
set p [layout := (row 2 [fill$widget i,widget b])]
set f [layout := (fill $ widget p)]
buttonEv w = showDialog quot;helloquot; quot;worldquot; w
showDialog:: String -> String -> Window a -> IO ()
showDialog msg title parent = do
mesd <- messageDialogCreate parent msg title wxOK
messageDialogShowModal mesd
messageDialogDelete mesd
● クリックされると関数が呼び出される
10. λ マルチスレッドとチャンネル
λλ
mainFrame = do
eventsChan <- newChan --MultiThread Queue
f <- frame [text := quot;titlequot;]
p <- panel f []
i <- textEntry p [text := quot;aquot;]
b <- button p [text:=quot;pushquot;,on command::=buttonEv i eventsChan]
set p [layout := (row 2 [fill$widget i,widget b])]
set f [layout := (fill $ widget p)]
timer <- windowTimerCreate f
timerOnCommand timer (onTimer eventsChan i)
timerStart timer 500 False
onTimer chan i = do
noEvents <- isEmptyChan chan
if noEvents then return ()
else do
str <- readChan chan;
set i [text := show$str]
buttonEv i chan w= do
forkOS child
set i [text:= quot;startquot;]
where
child = writeChan chan $fact 1000000
fact n=foldl' (*) 1 [1..n]
●
別スレッドからの信号をタイマーで受け取る
– (重い計算をするとどっち道固まったorz)