Pharo Smalltalk: Reading from TextMorph

2019-07-03 05:24发布

问题:

In Smalltalk using Pharo, I'm creating an application that reads the user input and does X.

So far I've managed to make a TextMorph that a user could enter a value into, but I'm unsure of how to read from TextMorphs and then do something with the value.

Any ideas?

Thanks

回答1:

Well, you can simply send text to your morph and get it's contents. So you could have a button and when button is pressed you do something with contents:

input := TextMorph new.
button :=
   SimpleButtonMorph new
      target: self
      actionSelector: #processTextMorph:;
      arguments: {input};
      yourself.

processTextMorph: aTextMorph
   | contents |
   contents := aTextMorph text.
   "do something with contents"

However maybe you want to use a dialog? Because you can do:

response := UIManager default request: 'What do you want to do?'.
response ifNotNil: [ "do something with the response" ]

And then the execution of UIManager default request: '…' will open a dialog with a text input