Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
how to express this event

Posted by warem (warem), 15 March 2005
hi everybody,

in the text widget, i want to use mouse to select some words, then push the right button and pop up a menu, in which has some menu items, then have some operations on the selected words. my question is how to express the event "use mouse to select some words".

thanks

Posted by admin (Graham Ellis), 16 March 2005
<Shift-Left> on a Text Widget lets you move the cursor and extend the selection.

<Shift-Button-1> Adjusts the end of the selection closest to the mouse as the mouse is moved

Posted by warem (warem), 16 March 2005
hi, Graham,

i am a novice.  what i want is:

bind .text <Shift-button-1> {popupRightbuttonMenu}

the "popupRightbuttonMenu" will deal with the texts you just selected with mouse. the "Shift-button-1" didn't work, neither did "Shift-Left". is anything wrong?

could you explain a little more about it?

thank you

Posted by admin (Graham Ellis), 17 March 2005
on 03/16/05 at 09:25:08, warem wrote:
hi, Graham,

bind .text <Shift-button-1> {popupRightbuttonMenu}

could you explain a little more about it?

thank you


That's saying that when button one on your mouse is pressed at the same time as the shift key, while the cursor is over the widget called .text, that you're going to run a proc called popupRightbuttonMenu.   By the way - should be "Button" rather than "button" - I think it's case sensitive here.

Some binding tags generate a whole series of events - they're typically the mouse movement ones - and others just generate a single event - for example when a key is pressed.  We've got a series of sample programs that use events on our site at http://www.wellho.net/resources/T224.html from where you can follow links to course examples - this is all covered on our public Tk course.

Posted by warem (warem), 17 March 2005
hi, Graham,

neither <Shift-Left> nor <Shift-Button-1> worked. maybe there is something wrong. what i want is:

1. use mouse to select some words,
2. push right button of mouse, then a popup-menu comes out. there are some items in the menu, for example, "copy", "paste". once choosing one item, for example, "paste", the selected words will be paste.

so, i think there are two events here:

1. how to express the event "selet some words by mouse"
2. push right button. for this i think it should be <ButtonRelease-2>

how about the first one? what's more, the popup-menu should come out after the above two actions, not only the first one.

please help me with it.

thanks

Posted by admin (Graham Ellis), 18 March 2005
If you can post up a simple example that sets up a text widget and has the code that isn't working for you to select a part of the text, I'll have a look and see if I can spot the problem.

Posted by warem (warem), 18 March 2005
hi, Graham,

here is the code:

+++++++++++++++++++++++++++++++++++++++
scrollbar .y -command ".t yview"
text .t -wrap word -width 80 -spacing1 1m -spacing2 0.5m -spacing3 1m \
       -height 25 -yscrollcommand ".y set"

pack .t -side left -fill both -expand yes
pack .y -side left -fill y

.t tag configure bold -font -*-Courier-Bold-O-Normal--*-120-*-*-*-*-*-*


Posted by warem (warem), 18 March 2005
hi, Graham,

here is the code:

+++++++++++++++++++++++++++++++++++++++
scrollbar .y -command ".t yview"
text .t -wrap word -width 80 -spacing1 1m -spacing2 0.5m -spacing3 1m \
       -height 25 -yscrollcommand ".y set"

pack .t -side left -fill both -expand yes
pack .y -side left -fill y

.t tag configure bold -font -*-Courier-Bold-O-Normal--*-120-*-*-*-*-*-*

.t insert end "Hello world." bold

bind .t <select-some-words-using-mouse> <ButtonRelease-2> {popupRightbuttonMenu}
+++++++++++++++++++++++++++++++++++++++++++++++++

the problem is in the last sentence.

thanks

Posted by admin (Graham Ellis), 19 March 2005
OK ... I'm guessing that popupRightbuttonMenu is one of your procs (A Google search for it reveals just one hit, and that's to this thread).  Do you have it working correctly under different events?

I'm going to be away from the system from which I test out Tcl issues like this for a couple of days, but if you can complete the example (with the best real data that you've tried but has failed) I can try it when I get back.  It would be a great help (and may even help you solve the issue) if you could (additionally) provide a complete worked example that pops up a menu in the way that you want but in the case of a different event - for example when you press a key on the keyboard, or when you enter focus.

Posted by warem (warem), 20 March 2005
hi, Graham,

below is my solution. it worked what i want. but i want to know if there is best solution.

+++++++++++++++++++++++++++++++++++++++
scrollbar .y -command ".t yview"
text .t -wrap word -width 80 -spacing1 1m -spacing2 0.5m -spacing3 1m \
  -height 25 -yscrollcommand ".y set"

pack .t -side left -fill both -expand yes
pack .y -side left -fill y

.t tag configure bold -font -*-Courier-Bold-O-Normal--*-120-*-*-*-*-*-*

.t insert end "Hello world. Something else..... (read from another file)" bold

bind .t <B1-Motion> {
   get_selection .t
   bind .t <Button-3> {right_button_menu .t}
}

proc get_selection {text} {
 global selected_word exist
 global SOUND_DIR sound_file

 set range [$text tag ranges sel]
 if {$range != ""} {
   set selected_word [$text get sel.first sel.last]
    set selected_word [string trimleft $selected_word " "]
   set selected_word [string trimright $selected_word " "]
   set init_letter [string range $selected_word 0 0]
   if {[regexp {[A-Z]*[a-z]*} $init_letter] == 0} {
     set exist 0
   } else {
     set exist 1
   }
   if {$exist == 1} {
     if {[regexp {[A-Z]*} $init_letter] == 1} {
       set init_letter [string tolower $init_letter]
       set word [string tolower $selected_word]
       set sound_file "$SOUND_DIR$init_letter/$word.wav"
     } else {
       set sound_file "$SOUND_DIR$init_letter/$selected_word.wav"
     }

 }

}

proc right_button_menu {text} {
 global selected_word exist
 global sound_file

 if {$selected_word != ""} {
   set right_popup_menu .text.menu
   catch {destroy $right_popup_menu}
   menu $right_popup_menu -tearoff 0
   bind $text <Button-3> {
     tk_popup .text.menu %X %Y
   }
   $right_popup_menu add command -label "Pronounce word \"$selected_word\"" -command {exec play $sound_file &} -underline 0
   if {$exist == 1} {
     $right_popup_menu entryconfig 0 -state active
   } else {
     $right_popup_menu entryconfig 0 -state disabled
   }
 }
}
+++++++++++++++++++++++++++++++++++++++


on the other hand, i want to know how to capture the word when double-clicking left mouse button. for example

bind .t <Double-1> {
 set word .......
}

how to write the ..... for the word selected by double-clicking the left mouse button.

Posted by admin (Graham Ellis), 21 March 2005
Double-clicks are written simply by writing the event twice, so
    <Escape><Escape>
is a double click on the ESC key and
     <Button-1><Button-1>
should be a double click on mouse button one.

You ask if there's a better way to code this ... I could pick up some minor things but I've found that code for this type of thing does tend to be a little longer and a bit messier that you might like. If it works wall and ain't bust, don't waste time fixing it  



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2021: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho