entry with new window - tcl

I want a new window appearing with simple entry to put one line of text and button ok to set value of variable. When I use simple entry command, it appear in my main window. I need something like tk_dialog with option to put text to some variable. Is there any predefined tk_* function?

you have to create another window with tk toplevel command
% set top [toplevel .top]
.top
% focus $top
% grab $top
% set entryBox [entry $top.ent -textvariable x]
.top.ent
% pack $entryBox
% set btn [button $top.btn -text "Click Me"]
.top.btn
% pack $btn

This procedure:
proc entrybox varName {
set top [toplevel .top[clock seconds]]
entry $top.eb -textvariable $varName
button $top.bu -command [list incr ${top}done] -text OK
pack {*}[winfo children $top]
vwait ::${top}done
unset -nocomplain ::${top}done
destroy $top
}
when given a global or fully qualified name, creates a new toplevel dialog with an entry and a button. It waits for the button to be clicked and then destroys the toplevel dialog. The text in the entrybox remains in the variable.
Documentation:
button (widget),
clock,
destroy,
entry (widget),
incr,
list,
pack,
proc,
set,
toplevel,
unset,
vwait,
winfo,
{*} (syntax)

Related

I am need select a option at the tk_menuOption and after invoke um event

What I want is for it to fire the event bind to each choice made.
So I will not use a button to call any event.
This is my The doubt:
set choice [list \
{News} \
{Nature} \
{City} \
{Food} \
{People} \
{Anime} \
{Tecnology} \
{Objects} \
{Others}]
tk_optionMenu . chose {*}$choice
bind . <1> {
clipboard clear
clipboard append [%W cget -text]
bell
# Procedure to be invoke, this bellow:
load
}
proc load { } {
# Code here!
tk_messageBox -message "You picked the option: $chose"
}
How to can I have some a event bind for tk_optionMenu, since that I can change first each option so to after do call procedure.
As in bind . <1>, there is no time to choose. Then, it is triggered by clicking on tk_menuOption.
I even tried to switch to other properties of the bind itself, but I didn't get the expected result.
I think something is missing to make it work as proposed in the question. I think it must be a stopwatch, which waits a few milliseconds before calling the event. This is what I imagine.
First off, tk_optionMenu is just a procedure.
Here's the source for it, but it's really quite short:
proc ::tk_optionMenu {w varName firstValue args} {
upvar #0 $varName var
if {![info exists var]} {
set var $firstValue
}
menubutton $w -textvariable $varName -indicatoron 1 -menu $w.menu \
-relief raised -highlightthickness 1 -anchor c \
-direction flush
menu $w.menu -tearoff 0
$w.menu add radiobutton -label $firstValue -variable $varName
foreach i $args {
$w.menu add radiobutton -label $i -variable $varName
}
return $w.menu
}
If it doesn't do what you want, just take the code and modify it.
Secondly, events on menus are really tricky! In particular, if you are on Windows or macOS then many standard events are not delivered at all due to the way that their native menuing system works. Instead, you get two possible, useful callbacks:
The -command callback on the menu items; this is supported by most of the menu items, especially including all the ones which end an interaction session with the menu (such as command items and radiobutton items).
The -postcommand callback on the overall menu which is called when the menu is posted to the screen, giving you an opportunity to alter the contents of the menu dynamically (if you need it).
If what you want can't be done with those, it probably shouldn't be done with a menu in the first place as the interaction pattern with menus is pretty strongly fixed. A listbox might be more suitable? Or something custom (you often can do those with a text or canvas; no C required).
While the tk_menuOption does not provide a binding for when an option is selected, it has an associated variable. So you can put a trace on that variable and do whatever you want in the handler:
trace add variable chose write {apply {{name arg op} {
upvar 1 $name var
clipboard clear
clipboard append $var
bell
# Procedure to be invoked
load
}}}
Note: It is not a good idea to overwrite built-in commands, like load. It is likely to break a lot of things.
Even though we already have the answers as good solutions that this problem addresses.
Before I even received the answer, everyone found this link - https://wiki.tcl-lang.org/page/tk_optionMenu
The description of the "snippet" in the link is as follows:
tk_optionMenu $w varName a b c d
for {set i 0} {$i < [[$w cget -menu] index end]} {incr i} {
[$w cget -menu] entryconfig $i -command $cmd
}
That was the discovery of the answer to solve my problem. So, I want to share with friends:
My Example:
#!/bin/sh
# \
exec wish "$0" "$#"
package require Tk
. configure -width 300 -height 200
set choice [list \
{News} \
{Nature} \
{City} \
{Food} \
{People} \
{Anime} \
{Tecnology} \
{Objects} \
{Others} ]
tk_optionMenu .foo chose {*}$choice
for {set i 0} {$i < [[.foo cget -menu] index end]} {incr i} {
[.foo cget -menu] entryconfig $i -command {load;bell}
}
proc load { } {
global chose
# Code here!
tk_messageBox -message "You picked the option: $chose"
}
pack .foo -side left -fill both -expand 1
.foo config -indicatoron 0 -relief flat -justify center -bg white
Print:
But, the answers given by them .. Schelte Bron and Donal Fellows, were big help. Now I learn more things that I didn't know before. Since the words load are embedded in Tcl, it is not a good idea to use them.

The tablelist curselection goes at calling the directory dialog

I have the test code:
package require Tk
package require tablelist
set ::tv {{N1 qwe} {N3 rty} {N4 uio}}
set ::dir [pwd]
tablelist::tablelist .tbl -columns {0 Name 0 Value} -listvariable ::tv
button .but -text "Directory..." -command {
set sel1 [.tbl curselection]
set sel2 [.tree selection]
tk_messageBox -message ".tbl curselection = \"$sel1\"\n\n.tree curselection = \"$sel2\""
set ::dir [tk_chooseDirectory -initialdir "$::dir"]
}
ttk::treeview .tree -columns Value
.tree heading "#0" -text "Name"
.tree heading "#1" -text "Value"
foreach t $::tv {
lassign $t t1 t2
.tree insert {} end -text $t1 -values $t2
}
.tbl selection set 0; #.tbl activate 0
.tree selection set I001
pack .tbl .tree .but -side left -anchor n -padx 9 -pady 9
At first pressing "Directory" button, I see "tablelist curselection=0" okay.
But at calling the directory dialog, the tablelist curselection disappears. The treeview selection remains, as it should be.
I couldn't find how to make the tablelist curselection to be untouched.
tablelist v6.8
TIA
This sort of weird behaviour is the kind of thing that occurs when several widgets are fighting over the PRIMARY selection, a feature of X11 which is used to mean that only one widget at a time has anything selected. (It also used to be used a lot for select-and-paste style text manipulation, but that's fallen out of favour and the more cross-platform CLIPBOARD style selections are used for that sort of thing.) I've no idea why it is turning on in your specific case and not elsewhere, but it's probably something to do with defaults in the X properties on Debian; that's not very visible but can be listed with this shell command.
xrdb -query -all
Tk widgets support the PRIMARY select by default (on X11; it's not really meaningful on other platforms) and many third-party and synthetic widgets do as well, but they can be told not to by setting the standard boolean -exportselection option on the widget to any false value. Once that's done, the widget continues to maintain a notion of what it has selected but does not export that notion outside of itself (unless you do Ctrl+C or something like that).
tablelist::tablelist .tbl -columns {0 Name 0 Value} -listvariable ::tv \
-exportselection false

how to get the value from ttk:entry

I am trying to get the value from ttk:entry. I have the following code.
variable DefaultRoot
ttk::label $wi.custcfg.dlabel -text "Default Root:"
ttk::entry $wi.custcfg.daddr -width 10 -textvariable ::DefaultRoot -validate focusout -validatecommand { puts $::DefaultRoot; return 1}
puts $DefaultRoot
But I am getting error on the last puts
The variable won't exist until you set it to some value. Merely declaring it as a variable (eg: variable DefaultRoot) won't make it spring into existence.
With the code you posted, you're executing the last puts about a microsecond after creating the entry widget. The user won't have the ability to enter any text before the puts happens. Thus, the variable won't yet exist and the puts will fail.
A simple solution is to make sure to set the variable before you call puts, though that only means that the puts will print the default value.
In other words, this will print "this is the default":
variable DefaultRoot
set DefaultRoot "this is the default"
ttk::entry $wi.custcfg.daddr -textvariable ::DefaultRoot
puts $DefaultRoot
To answer your specific question, however, you can use $::DefaultRoot anywhere you want after the variable has been created.
For example, you could create a button that prints the value like this:
proc print_variable {} {
puts "DefaultRoot=$::DefaultRoot"
}
ttk::button $wi.custcfg.button -text foo -command print_variable
You can access the variable anywhere via
global DefaultRoot
puts $DefaultRoot
or
puts $::DefaultRoot

Create and place widget from subroutine

My form is a basic two label frames with one of them containing check boxes and the other is an image. Below these two frames is a back and a start button. The window is preset so that it cannot be altered but when the start button is pressed additional widgets appear on the screen. However, I would like the widgets to only be created and placed on the screen after the checkbox has been selected and the start button has been clicked. The start button then calls a function called "Balanced". Within this code it creates the new widgets and places them on the window. However, it returns an error: "bad window path name '.lblfrmProgress'"
#Set Dual UTA Window as top-level
set UTA .dual_uta
wm state . withdrawn
catch {destroy $UTA}
toplevel $UTA
#Window Properties
wm title $UTA {Device: Dual UTA}
wm maxsize $UTA 522 231 ;#x-500, y-231
wm minsize $UTA 522 231 ;#x-500, y-231
The above is a section of the code that creates a window under the alias of UTA. I thought that this window is the top-level window and as such could be referenced using $UTA.[pathname].
global UTA .dual_uta
#**************** DO NOT MODIFY - USER INTERFACE CODE *******************
#Setup window with labels to show progress
labelframe $UTA.lblfrmProgress -text "Test Progress" -padx 1 -relief groove -height 145 -width 520
label $UTA.lblUTASetup -text "Dual UTA setup according to image"
label $UTA.lblVQuadStart -text "VQuad is initializing"
label $UTA.lblVQTStart -text "VQT is initializing"
label $UTA.lblLMC -text "Load 'Balanced' Master Configuration"
label $UTA.lblTxRx1 -text "Side 1 Tx - Side 2 Rx"
label $UTA.lblTxRx2 -text "Side 1 Rx - Side 2 Tx"
Am I referencing the window variable name incorrectly? Do I need to pass the window variable via procedure call? I just call the file by using 'source Balanced.tcl'
Thanks for the help!
Your use of global appears to be somewhat off. In particular, each argument to global is the name of a variable to map in; initialization should be done separately. Or you can both bring the variable in and (optionally) initialize it with the variable command:
proc whatever {} {
variable UTA .dual_uta
destroy $UTA; # No error if $UTA doesn't exist
toplevel $UTA
wm title $UTA {Device: Dual UTA}
labelframe $UTA.lblfrmProgress -text "Test Progress" \
-padx 1 -relief groove -height 145 -width 520
# Etc.
}
It's usually considered better to use that form of variable only within the enclosing namespace (i.e., the global namespace, ::, unless you say otherwise) and only use the single argument form inside a procedure.
variable UTA .dual_uta
proc whatever {} {
variable UTA
destroy $UTA
toplevel $UTA
wm title $UTA {Device: Dual UTA}
labelframe $UTA.lblfrmProgress -text "Test Progress" \
-padx 1 -relief groove -height 145 -width 520
# Etc.
}
Myself, I'd structure the procedure so that the “root name” of the window hierarchy to build was a parameter to the procedure, binding the name into any callbacks during creation:
proc whatever {UTA} {
destroy $UTA
toplevel $UTA
wm title $UTA {Device: Dual UTA}
labelframe $UTA.lblfrmProgress -text "Test Progress" \
-padx 1 -relief groove -height 145 -width 520
# Etc.
button $UTA.thingamijig -text "Fluffy Bunny" -command [list doTheCallback $UTA]
# ...
}
I'd also be saving the names of widgets in variables for use in later pack/grid calls, so as to avoid having to write long widget paths quite so often. It's just slightly more mnemonic IMO, but certainly not necessary. (Binding the pathnames into callbacks à la the use of list above instead of using a global/namespace variable is better style though, and less problematic than writing callbacks with string substitutions.)
Do you create the UTA variable in a proc? If so, you have to declare it global there too.
The global command takes one or more variable names, so global UTA .dual_uta doesn't make sense.

Creating a Tk Tcl entry that acts like a label

I want to create labels that the text in them can be selected for copy/paste. To do this I tried to use entries that are read-only. But I can't seem to initialize the text value in them. The labels are generated inside a loop and the number of labels and their content is unknown. The code to produce the labels is:
proc test_labels {} {
toplevel .labels
# Main Frame
frame .labels.main_frame -relief "groove" -bd 2
pack .labels.main_frame
set r 1
foreach t [list banana apple grapes orange lemon peach] {
set lbl [label .labels.main_frame.lbl_$r -text "fruit $r:"]
set lbl2 [label .labels.main_frame.val_$r -text $t]
grid $lbl -row $r -column 1 -sticky nse
grid $lbl2 -row $r -column 2 -sticky nsw
incr r
}
set ok_btn [button .labels.main_frame.ok_b -text "OK" -command {prop_menu_ok_button}]
grid $ok_btn -row [expr $r+2] -column 1 -columnspan 2 -sticky nsew
grab release .
grab set .labels
center_the_toplevel .labels
bind .labels <Key-Return> {test_labels_ok_button}
}
And it creates the fallowing window:
Then I try to replace the line set lbl2 [label .labels.main_frame.val_$r -text $t] with the lines:
eval "set text_val_$r $t"
eval "set lbl2 [entry .labels.main_frame.val_$r -relief flat -state readonly -textvar text_val_$r]"
But this only creates empty lines:
How can I put default values to entry widgets?
Related to the question How to make the text in a Tk label selectable?
These lines are almost certainly not what you want! (If you're using eval, you should always ask whether it's really necessary; from 8.5 onwards, the likely answer is “it's not necessary”.)
eval "set text_val_$r $t"
eval "set lbl2 [entry .labels.main_frame.val_$r -relief flat -state readonly -textvar \$\{text_var_$r\}]"
The key problem — apart from the use of eval — is that the -textvariable option takes the name of a variable. Let's fix that by using an array to hold the values:
set text_val($r) $t
set lbl2 [entry .labels.main_frame.val_$r -relief flat -state readonly \
-textvariable text_val($r)]
Also, be aware that the text_val array needs to be global (or in a namespace, if you fully qualify the name when giving it to the -textvariable option). This is because it is accessed from places which are outside the scope of any procedure.
Of course, it turns out that if we are keeping values constant then we can avoid using a variable at all and just insert the value manually.
set lbl2 [entry .labels.main_frame.val_$r -relief flat]
$lbl2 insert 0 $t
$lbl2 configure -state readonly
If you're never changing the value, that will work fine.