从Prolog在PCE中的设置值很容易,使用send/3或send/4。所以解决这个问题的方法之一就是让Prolog谓词调用一个方法来在PCE对象上设置一个值。
对象可以包含其他对象,并且具有范围内的类和实例变量。所有Prolog代码需要的是对对象的引用:通过使用对象引用调用谓词,谓词可以调用适当的方法来传递值。
以下是一些基于对话框类创建对象的代码。对象中的一个按钮在按下时将调用谓词(类似于此问题中的谓词),该谓词将根据传入的值创建一个值,然后将该值设置为对话框的实例变量 。它还会将该值放在文本项目控件中。
此程式的来源是textfield。PL并且可以与
swipl -s textfield.pl -g run
默认文本运行:
用户输入
文本经由来自谓词发送到GUI的消息由程序更新调用按下按钮:
的演示类:
%%% Create a new value based on the input string
%%% Write the new value back to the 'input' member of the
%%% 'demo' object. Also put the value int the 'result' slot.
doIt(Demo,Word) :-
concat_atom(['*** ' , Word, ' *** '] ,WordGotDid),
format("doIt: Setting result: ~w...~n", [WordGotDid]),
get(Demo,member,input,InputText),
send(InputText,selection,WordGotDid),
send(Demo,slot,result,WordGotDid).
%%% Read and display the 'result' slot.
printIt(Demo) :-
get(Demo,slot,result,Result),
write('\nResult: "'),
write(Result),
write('"\n').
主要程序:
%%% Create an object that has fields that can be mutated.
run :- new(Demo,demo),
send(Demo,open).
看演示和编码之后由按钮叫
:- use_module(library(pce)).
:- pce_begin_class(demo, dialog).
/* Instance variables*
name, type, access, description */
variable(result, name*, get, "Result from Prolog Callback").
initialise(Demo) :->
"Create something that get/4 and send/3 can work with."::
send(Demo, send_super, initialise, 'Demo'),
send(Demo, append, new(InputText, text_item(input, 'Demo Text'))),
send(Demo, append,
button(execute_command,
and(message(@prolog,doIt, Demo,InputText?selection),
message(@prolog,printIt,Demo)))).
:- pce_end_class.
代码一,这是我的意见,可以通过ange,一旦我完成了对XPCE的学习:尽管通过XPCE的消息编程可以在Prolog中进行编程,但是看看演示代码,这不是它完成的方式。编程是在Prolog或其他语言中完成的,并且用Prolog作为粘合剂,而XPCE主要是被动GUI,有点像HTML表单。该程序创建XPCE对象,更改它们的状态并从中读取值。除了一些与GUI相关的小型消息传递外,XPCE通常不会写入该程序;但即使这样做通常是在XPCE对象的方法中完成的。
谢谢,我会检查一下 - 现在我通过在prolog中使用send/3解决了我的问题,正如您在第一行中所建议的那样... – Matthias 2010-12-12 18:05:16