> From: Dario Lopez-Kästen
> Some totally clueless newbie questions for the community...
> First some background:
> I am finally taking time to explore wxPython and PythonCard. I have a
> question regarding PythonCard. I've worked quite extensivley with mac
> applications like HyperCard, FaceSpan and REALBasic, when I was younger
> (some 6 years ago :-) From a quick glance, it seems that
> PythonCard follows
> the HyperCard model very closely.
> Question 1:
> Are there any plans to move on to be more like FaceSpan or REALBasic?
Could you be more specific? I don't know what FaceSpan is. As to REALBasic,
do you mean a more similar language, environment, or something else.
> Question 2:
> Also, I have a question related to what kind of object model is being used
> in PythoCard. In both FaceSpan and Hypercard I could, using
> AppleScript/HyperTalk, use idioms along the lines of (pseudo code):
> get text of field 'TextField'
> put it in field id 2
> hilite word 3 of text of field id 2
PythonCard uses Python as its language, so no xTalk equivelant is planned.
I'll give you a simple example in the shell using the widgets sample (run
widgets.py with the -s command-line option) using text from the fldTextArea
and fldTextField fields. The first two lines of the code above would be done
>>> it = comp.fldTextArea.text
>>> comp.fldTextField.text = txt
or could just be simplified to
>>> comp.fldTextField.text = comp.fldTextArea.text
All components in PythonCard are referenced by name, the components have
internal ids, but those are never used in user code, so HyperTalk references
like "field 2" or "field id 1608" aren't used. Since Python uses lists and
dictionaries, most of the things you would have done with numeric ids can
just be done with a list or dictionary reference.
The last line of the code above is the tricky part. PythonCard sits on top
of wxPython, and the underlying wxPython control (wxTextCtrl) has no concept
of words or paragraphs. The best solution would probably be to have some
generic Python text routines for "word" and "paragraph" and then use those
to figure out character offsets. Wrapper methods could be added to the
TextField component to simplify those sorts of operations. Anyway, here is a
simplistic function for getting the starting and ending character offsets of
a particular word in some text. n is zero-based; this code probably has some
bugs, I only did a simple test.
def wordOffsets(txt, n):
words = txt.split()
if len(words) < n:
start = 0
for i in range(n):
start += len(words[i]) + 1
end = start + len(words[n]) - 1
return [start, end]
The relevant method in PythonCard/wxPython that we want to use is
SetSelection. From the wxPython help...
"Selects the text starting at the first position up to (but not including)
the character at the last position. If both parameters are equal to -1 all
text in the control is selected."
So, we get our offsets with the function above and then add 1 to the end
offset to make the selection. Finally, since a selection isn't normally
visible unless the field has focus, we make sure that it does.
>>> sel = wordOffsets(it, 2)
>>> comp.fldTextField.SetSelection(sel, sel + 1)
Click on the Widgets Test title bar and you should see the correct
> set the hilite of button 1 of card 2 to true
> set the name of button 1 to "Default Button"
I can't remember the purpose of setting the hilite of a button in HyperCard.
Perhaps, this is another focus issue, in which case you just want SetFocus
If you are wanting a toggle button I do have one, but for some reason it
isn't checked into cvs. I see that Yao Heling [hyao@...] submitted this
componet, but I don't know why I didn't just add it. Anyone want to refresh
In PythonCard, the name is the unique id of a component, so you don't
generally change that. If you simply want to change the label of the button
then you use the label attribute. Something like:
>>> comp.btnButton.label = "Default Button"
> this was possible because of the object model that those apps provided for
> them selves or the object/widgets they used. Is something possible in
> PythonCard? if not, is it planned?
> Thank you for your patience.
> - --------------------------------------------------------------------
> Dario Lopez-Kästen, IT Systems & Services Chalmers University of Tech.
Does this answer your questions?