Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need help creating a command button...

Status
Not open for further replies.

WTHolmes

Technical User
Mar 22, 2007
18
0
0
US
Hi, I am returning to the well; this time I am trying to:

1. Create a command button, which will launch a dialog box.

2. The dialog box will have two buttons.

3. When you move the cursor over the button the text on
the button will change from one thing to another and
stay that way for a set number of seconds with no click.
(if that is possible to have a "moveover" option)

I already have some code written and would like to just drop this added bit in. My existing code is as follows:

proc push_button {} {
... whatever ...
}
button .but -text "Push Me" -command "push_button"
pack .but

after you click on the button I would like the forementioned to take place.

Thanking you in advance,

W. T. Holmes
 
I think it depends on what you use for a "dialog". tk_dialog interrupts execution until a button is pushed so no other script commands will be executed until you push the button. You could build your own with toplevel. In that case I'd look at the Button Enter event for bindings.

_________________
Bob Rashkin
 
Thank You Bong,

In the event that execution is suspended, I would like to go with a "click-pause" solution. After the button is clicked, I would like the button's text to change from "Click me" to "Clicked" after a few seconds, I would like it to "reset" itself by changing the text back to "Click me."

Thanks!
 
Well, you still can't use tk_dialog, as far as I know. Once the button is clicked, the dialog disappears. If you build your own dialog, with either a new frame or a toplevel, you can control the behavior completely.
Code:
proc push_button {} {
    toplevel .tp1 
    pack [label .tp1.lb1 -text "your text here"] -side top
    pack [button .tp1.b1 -text "Click Me" -command bttn2
}
proc bttn2 {} {
    .tp1.b1 configure -text Clicked
    after 4000
    .tp1.b1 configure -text "Click Me Again"
}


_________________
Bob Rashkin
 
Thanks Again Bong,

Maybe I am doing something wrong. I am brand new to Tcl/Tk I thought it would be a good starting point for GUI programming. I cut and paste your code into a text document and saved it with a .tl extension and ran it with Wish. I got nothing but a blank Wish window on the screen. Am I doing something wrong?

Thanks again,

W. Holmes
 
My code comprises 2 procs, one called by the other. Even so, something has to call the first proc. In your original post, you had created a button that calls the first proc, push_button. I figured we could assume you'd start from there.

_________________
Bob Rashkin
 
Thank You agan Bong,

I guess I am just stupid, I still can't get it to work. So I thought I would give you what I have and maybe you can help me get it working.

Thanks again,

WTHolmes


proc menu_clicked { no opt } {
tk_messageBox -message \
"You have clicked $opt.\nThis function is not implanted yet."
}

#Declare that there is a menu
menu .mbar
. config -menu .mbar

#The Main Buttons
.mbar add cascade -label "Menu1" -underline 4 \
-menu [menu .mbar.file -tearoff 0]
.mbar add cascade -label "Menu2" \
-underline 4 -menu [menu .mbar.oth -tearoff 1]
.mbar add cascade -label "Menu3" -underline 4 \
-menu [menu .mbar.help -tearoff 0]

## Menu1 Menu ##
set m .mbar.file
$m add command -label "M1-L1-1" -underline 6 \
-command { .txt delete 1.0 end } ;# A new item called New is added.
$m add checkbutton -label "M1-L1-2" -underline 6 -command { menu_clicked 1 "M1-L2-2" }
$m add command -label "M1-L1-3" -underline 6 -command { menu_clicked 1 "M1-L2-3" }
$m add separator
$m add command -label "Quit" -underline 0 \
-command {set answer [tk_messageBox -message "Really Quit?" -type yesno -icon question]
switch -- $answer {
yes exit
no {tk_messageBox -message "I know you can't resist me!" -type ok}
}
}

## Menu2 Menu ##
set m .mbar.oth
$m add cascade -label "M2-L1-1" -underline 0 -menu [menu $m.mnu -title "M2-L1-1"]
$m.mnu add command -label "M2-L2-1" -underline 6 \
-command { .txt insert end "You have chosen M2-L2-1\n"}
$m.mnu add command -label "M2-L2-2" -underline 6 -command { \
.txt insert end "You have chosen M2-L2-2\n"}
$m.mnu add command -label "M2-L2-3" -underline 6 \
-command { .txt insert end "You have chosen M2-L2-3\n"}
$m add command -label "All" -underline 0 \
-command { .txt insert end {You have chosen All}
}

## Menu3 ##
set m .mbar.help
$m add command -label "M3-L1-1" -underline 0 -command {
.txt delete 1.0 end
.txt insert end {
You have chosen M3-L1-1}
}
proc push_button {} {
toplevel .tp1
pack [label .tp1.lb1 -text "your text here"] -side top
pack [button .tp1.b1 -text "Click Me" -command bttn2
}
proc bttn2 {} {
.tp1.b1 configure -text Clicked
after 4000
.tp1.b1 configure -text "Click Me Again"
}

button .but -text "Push Me" -command "push_button"
pack .but


#Making a text area
text .txt -width 55
pack .txt
 
Code:
proc menu_clicked { no opt } {
    tk_messageBox -message \
        "You have clicked $opt.\nThis function is not implanted yet."
}

#Declare that there is a menu
menu .mbar
. config -menu .mbar

#The Main Buttons
.mbar add cascade -label "Menu1" -underline 4 \
      -menu [menu .mbar.file -tearoff 0]
.mbar add cascade -label "Menu2" \
      -underline 4 -menu [menu .mbar.oth -tearoff 1]
.mbar add cascade -label "Menu3" -underline 4 \
      -menu [menu .mbar.help -tearoff 0]

## Menu1 Menu ##
set m .mbar.file
$m add command -label "M1-L1-1" -underline 6 \
      -command { .txt delete 1.0 end } ;# A new item called New is added.
$m add checkbutton -label "M1-L1-2" -underline 6 -command { menu_clicked 1 "M1-L2-2" }
$m add command -label "M1-L1-3" -underline 6 -command { menu_clicked 1 "M1-L2-3" }
$m add separator
$m add command -label "Quit" -underline 0 \
-command {set answer [tk_messageBox -message "Really Quit?" -type yesno -icon question]
switch -- $answer {
    yes exit
    no {tk_messageBox -message "I know you can't resist me!" -type ok}
}
}

## Menu2 Menu ##
set m .mbar.oth
$m add cascade -label "M2-L1-1" -underline 0 -menu [menu $m.mnu -title "M2-L1-1"] 
  $m.mnu add command -label "M2-L2-1" -underline 6 \
       -command { .txt insert end "You have chosen M2-L2-1\n"}
  $m.mnu add command -label "M2-L2-2" -underline 6 -command { \
     .txt insert end "You have chosen M2-L2-2\n"}
  $m.mnu add command -label "M2-L2-3" -underline 6 \
       -command { .txt insert end "You have chosen M2-L2-3\n"}
$m add command -label "All" -underline 0 \
    -command { .txt insert end {You have chosen All}
      }

## Menu3 ##
set m .mbar.help
$m add command -label "M3-L1-1" -underline 0 -command { 
    .txt delete 1.0 end
    .txt insert end {
    You have chosen M3-L1-1}
    }
proc push_button {} {
toplevel .tp1 
    pack [label .tp1.lb1 -text "your text here"] -side top
    pack [button .tp1.b1 -text "Click Me" -command [red]{.tp1.b1 configure -text Clicked; bttn2}][/red]
}
proc bttn2 {} {
    [red]update[/red]
    after 4000
    .tp1.b1 configure -text "Click Me Again"
}

button .but -text "Push Me" -command "push_button"
pack .but


#Making a text area
text .txt -width 55
pack .txt

_________________
Bob Rashkin
 
Awesome!!!!!!

Bong once again, you have proven yourself "Da Man!"

Thank You!
 
Hi Bong,

It's me again. I would like to add a cancel button to the right of the click me/clicked button that you created. This cancel button will close the tp1 window and leaving just the main window open. I have included your code below to make it easier.

I want to thank you in advance, with your help, I am learning Tcl/Tk.

Thanks again,

WTHolmes

proc menu_clicked { no opt } {
tk_messageBox -message \
"You have clicked $opt.\nThis function is not implanted yet."
}

#Declare that there is a menu
menu .mbar
. config -menu .mbar

#The Main Buttons
.mbar add cascade -label "Menu1" -underline 4 \
-menu [menu .mbar.file -tearoff 0]
.mbar add cascade -label "Menu2" \
-underline 4 -menu [menu .mbar.oth -tearoff 1]
.mbar add cascade -label "Menu3" -underline 4 \
-menu [menu .mbar.help -tearoff 0]

## Menu1 Menu ##
set m .mbar.file
$m add command -label "M1-L1-1" -underline 6 \
-command { .txt delete 1.0 end } ;# A new item called New is added.
$m add checkbutton -label "M1-L1-2" -underline 6 -command { menu_clicked 1 "M1-L2-2" }
$m add command -label "M1-L1-3" -underline 6 -command { menu_clicked 1 "M1-L2-3" }
$m add separator
$m add command -label "Quit" -underline 0 \
-command {set answer [tk_messageBox -message "Really Quit?" -type yesno -icon question]
switch -- $answer {
yes exit
no {tk_messageBox -message "I know you can't resist me!" -type ok}
}
}

## Menu2 Menu ##
set m .mbar.oth
$m add cascade -label "M2-L1-1" -underline 0 -menu [menu $m.mnu -title "M2-L1-1"]
$m.mnu add command -label "M2-L2-1" -underline 6 \
-command { .txt insert end "You have chosen M2-L2-1\n"}
$m.mnu add command -label "M2-L2-2" -underline 6 -command { \
.txt insert end "You have chosen M2-L2-2\n"}
$m.mnu add command -label "M2-L2-3" -underline 6 \
-command { .txt insert end "You have chosen M2-L2-3\n"}
$m add command -label "All" -underline 0 \
-command { .txt insert end {You have chosen All}
}

## Menu3 ##
set m .mbar.help
$m add command -label "M3-L1-1" -underline 0 -command {
.txt delete 1.0 end
.txt insert end {
You have chosen M3-L1-1}
}
proc push_button {} {
toplevel .tp1
pack [label .tp1.lb1 -text "your text here"] -side top
pack [button .tp1.b1 -text "Click Me" -command {.tp1.b1 configure -text Clicked; bttn2}]
}
toplevel .tp2
pack [button .tp2.b2 -text "Cancel" -command {.tp2.b2 configure -text Cancel; bttn3}]
proc bttn2 {} {
update
after 10000
.tp1.b1 configure -text "Click Me Again"
}

button .but -text "Push Me" -command "push_button"
pack .but


#Making a text area
text .txt -width 55
pack .txt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top