I want to bind an element so that when ever a mouse hovers over it, it will do something. What modifier allows such bind?
The <Enter> and <Leave> events. For example:
place [frame .f -width 100 -height 100 -bg red] -x 10 -y 10
bind .f <Enter> {%W configure -bg blue}
bind .f <Leave> {%W configure -bg red}
Related
I created a gui with many tabs, i would like to add button to the tab1.
However i am not good at tcl, Could someone help how to add button to the TCL gui?
Regards
toplevel .test
wm transient .test.
set pw [ttk::panedwindow .test.pw -orient vertical]
set nb [ttk::notebook $pw.nb]
foreach i {1 2 3 4} {$nb add [frame $nb.f$i] -text tab$i}
set fTkCon [frame $pw.fTkConContainer -container 1]
$pw add $nb
$pw add $fTkCon
pack $pw -fill both -expand
#add button here# ::hwtk::button -text "Text Button" -help "Text only"
In the foreach loop, you created 4 frames ($nb.f1 through $nb.f4). To add a button in tab1, you should normally create the widget as a child of the respective frame. You then need to use a geometry manager to control where the widget will appear. When just starting with GUIs, grid is probably the easiest choice.
ttk::button $nb.f1.b1 -text Button! -command {puts Pressed!}
grid $nb.f1.b1 -padx 5 -pady 5
Note: When doing this in an interactive session, you will probably have to resize the window and move the sash of the paned window to see the button.
I simplified the code a little to make it run:
package require Tk
ttk::notebook .nb
pack .nb
foreach i {1 2 3 4} {
.nb add [button .nb.f$i -text "Button $i"] -text tab$i
}
Notice to add a button to a tab, I exchanged the creation of frame in the notebook add command to creation of a button.
I want an entry widget with some default text shown in the box. -state disable. When I click on edit button, it will be enable and I can able to change the text. I can manage the Edit button portion, just need help about the entry widget.
I have tried with this code :
entry .e1 -text "abcd" -state disable
pack .e1 -in .WorkArea -side left
frame .workArea
pack .workArea
Note that the -text option is an abbreviation of -textvariable, i.e. the name of a global variable that contains the text entered into the entry widget. Setting that variable to a value gives the entry some text.
entry .e1 -textvariable abcd -state disabled
set abcd wxyz
pack .e1 -in .workArea -side left
You now need a button that configures the entry widget to be normal (enabled) when it is pushed:
button .b1 -text Enable -command {.e1 configure -state normal}
pack .b1 -in .workArea -side left
Documentation:
button (widget),
entry,
frame (widget),
pack,
set
Is it possible to display the widget hint on mouseover of widget ?
Every Tk widget is sent an <Enter> event when the mouse pointer goes over it, and is sent a <Leave> event when the mouse pointer goes elsewhere.
# Make some widgets; the buttons are much larger than the status text
pack [button .b1 -text "First button" -font {Arial 24}]
pack [button .b2 -text "Second button" -font {Arial 24}]
pack [label .l1 -textvariable status -font {Arial 10}]
# Set up some simple bindings
bind .b1 <Enter> {set status "Over the first button"}
bind .b1 <Leave> {set status ""}
bind .b2 <Enter> {set status "Over the second button"}
bind .b2 <Leave> {set status ""}
That's the core of how you do this sort of thing. The other major thing to note is that when you click on a widget, a temporary grab is set so that all (mouse-related) events get sent to that widget until the mouse button is released. If you're wanting to work out what widget the mouse is over and you've not got it directly from the event, the winfo containing command is exactly the right tool.
You can use the tooltip package which should be among the default packages of Tcl.
package require tooltip
pack [label .l -text "Hover your mouse over me!"]
tooltip::tooltip .l "I'm a helpful hint!"
Reference:
wiki
manual
I'm looking for a predefined tk window like tk_messageBox where a user can input a string in a white line. I couldn't find one in the Tcl / Tk man page (https://www.tcl.tk/man/tcl/TkCmd/contents.htm)...
There's no predefined popup text-entry window. You'll need to make one yourself with a toplevel, an entry and (probably) at least one button. Maybe a label too.
Here's the simplest thing that could work at all:
set foo "This is some text."
toplevel .t
pack [entry .t.e -variable foo]
pack [button .t.b -text "OK" -command {destroy .t}]
bind .t <Return> {.t.b invoke}
focus .t.e
tkwait window .t
puts "The variable contains '$foo'"
You will probably need to customise it further…
I want to create a new window when a particular button is pressed and the newly created window should contain labels/entries/buttons. My code goes something like this..
. configure -width 400 -height 400
label .header -text "Bitfields"
place .header -x 5 -y 0
.................................
toplevel .window -width 100 -height 120
Now I want to create a button/label on the newly created window . How am I supposed to do that? Google mostly provides examples for tkinter which I think is linked to python which I am not using. As a sub question how can I make this window appear when a button is clicked from the parent window?
To create a button/label on the newly created window (called .window):
button .window.button1 -text "ok"
To make a window appear when a button is clicked from the parent window:
proc showWindow {w} {
catch {destroy $w}
toplevel $w
button $w.button1 -text "ClickMe"
pack $w.button1
}
. configure -width 400 -height 400
button .header -text "Bitfields" -command "showWindow .window"
place .header -x 5 -y 0