Integrate Tk grid widget into notebook layout - user-interface

I want to quickly prototype a GUI and thought that Tk could be simple to learn. However, I failed to integrate a sample grid view (frame) into one of the tabs of the notebook widget. The pack command places the grid on top of the notebook and I could not figure out the correct option. Or is my approach anyhow wrong?
Here is the code:
ttk::frame .c
ttk::frame .c.f -borderwidth 5 -relief sunken -width 200 -height 100
ttk::label .c.namelbl -text Name
ttk::entry .c.name
ttk::checkbutton .c.one -text One -variable one -onvalue 1; set one 1
ttk::checkbutton .c.two -text Two -variable two -onvalue 1; set two 0
ttk::checkbutton .c.three -text Three -variable three -onvalue 1; set three 1
ttk::button .c.ok -text Okay
ttk::button .c.cancel -text Cancel
grid .c -column 0 -row 0
grid .c.f -column 0 -row 0 -columnspan 3 -rowspan 2
grid .c.namelbl -column 3 -row 0 -columnspan 2
grid .c.name -column 3 -row 1 -columnspan 2
grid .c.one -column 0 -row 3
grid .c.two -column 1 -row 3
grid .c.three -column 2 -row 3
grid .c.ok -column 3 -row 3
grid .c.cancel -column 4 -row 3
# Notebook --> shall contain above grid in third tab
ttk::notebook .n -width 600 -height 200
ttk::frame .n.f1;
ttk::frame .n.f2;
.n add .n.f1 -text "FirstTab"
.n add .n.f2 -text "SecondTab"
.n add .c -text "GridContent"
pack [label .n.f1.f1 -background red -foreground white -text "First"]
pack [label .n.f2.f2 -background red -foreground white -text "Second"]
pack .c
pack .n
ttk::notebook::enableTraversal .n

The content widgets of a ttk::notebook must be higher in the stacking order than the notebook for it to work correctly, and must be managed by the notebook itself and not by pack or grid (though their contents can be managed any way you want); notebooks are a special kind of geometry manager as well as being a widget. (Tk has several other widgets that do this too.)
To fix the stacking order, either create the .c widget after the .n widget, or raise .c after creating .n. Note that a parent widget's children (except for toplevels, and menus in a few circumstances) always come on top of the parent, and are always bounded/clipped by their parent.
To fix the management issue, simply don't pack .c; adding it to the notebook is enough. You may pack or grid the contents of .c as you see fit.
With those two small fixes, your UI appears to work.
ttk::frame .c
ttk::frame .c.f -borderwidth 5 -relief sunken -width 200 -height 100
ttk::label .c.namelbl -text Name
ttk::entry .c.name
ttk::checkbutton .c.one -text One -variable one -onvalue 1; set one 1
ttk::checkbutton .c.two -text Two -variable two -onvalue 1; set two 0
ttk::checkbutton .c.three -text Three -variable three -onvalue 1; set three 1
ttk::button .c.ok -text Okay
ttk::button .c.cancel -text Cancel
grid .c -column 0 -row 0
grid .c.f -column 0 -row 0 -columnspan 3 -rowspan 2
grid .c.namelbl -column 3 -row 0 -columnspan 2
grid .c.name -column 3 -row 1 -columnspan 2
grid .c.one -column 0 -row 3
grid .c.two -column 1 -row 3
grid .c.three -column 2 -row 3
grid .c.ok -column 3 -row 3
grid .c.cancel -column 4 -row 3
# Notebook --> shall contain above grid in third tab
ttk::notebook .n -width 600 -height 200
ttk::frame .n.f1;
ttk::frame .n.f2;
.n add .n.f1 -text "FirstTab"
.n add .n.f2 -text "SecondTab"
.n add .c -text "GridContent"
raise .c; # <<<< YES! YOU DO WANT THIS! <<<< YES! <<<< YES! <<<< YES! <<<<
pack [label .n.f1.f1 -background red -foreground white -text "First"]
pack [label .n.f2.f2 -background red -foreground white -text "Second"]
# pack .c; # <<<< NO! YOU DO NOT WANT THIS! <<<< NO! <<<< NO! <<<< NO! <<<<
pack .n
ttk::notebook::enableTraversal .n

Related

How to plot line in xmgrace using Bash or shell script

I want to plot a vertical dotted red line in particular point (say 2.2) in xmgrace using script
You can either use an external tool such as pygrace or use grace's built-in batch capabilities. These have been touched upon here on SO before (see, for instance, here or here).
The following script plots a datafile (exp.dat) as empty circles, another data file (line.dat) as a red dotted line and sets the ranges, labels and major ticks of the two axes:
READ NXY "exp.dat"
READ NXY "line.dat"
WORLD XMIN 0
WORLD XMAX 5
WORLD YMIN 1
WORLD YMAX 5
xaxis label "My x label"
xaxis tick major 1
yaxis label "My y label"
yaxis tick major 1
s0 line type 0
s0 symbol 1
s0 symbol size 1.5
s1 linestyle 2
s1 color 2
To generate a vertical red dotted line that passes through 2.2 the contents ofline.dat should be something like
2.2 0
2.2 10000
You can generate such a file in a bash script with the following command:
echo "2.2 0\n2.2 10000" > line.dat
Save the script as mybatch.xmg and call it like this:
xmgrace -batch mybatch.xmg
If you want to directly generate an output you can add this directive:
PRINT TO "myplot.eps"
DEVICE "EPS" OP "level2"
PRINT
which will save your plot as myplot.eps (add -nosafe when calling xmgrace to get rid of the warnings).

How can solve the code of a rotated histogram?

Hi to whole stackoverflow group,
I am having a series of problems when defining the axes range in my graphic
and format in general, and I would like to share it with you to see if among all we can find the error
I have found on this website a user who has made it similar. My idea is to have something similar like the graphic of the link below. But for some reason, it's probably silly, it does not appear correctly.
My code is as following:
set term post eps enhanced color "Times-Roman" 14
set output "ComparacionPurezaMetodos.eps"
set key off
set style data histogram
set style histogram cluster gap 1
set style fill solid border -1
set boxwidth 0.8
set xtic rotate by 90 scale 0
unset ytics
set y2tics rotate by 90
set y2label 'Ti_3SiC_2 content, wt{/Symbol\045}' offset -2.5
#set xlabel ' '
set size 0.6, 1
set label 'Powder mixture' at graph 0.5, -0.1 centre rotate by 180
plot 'datos.txt' using 1:(-1):3 with labels rotate right, 'datos.txt' using 1:2 with boxes
and the data are very simple:
# index, purity, name
1 98 Ti/Si/TiC
2 94 Ti/TiSi_2/TiC
3 93.6 Ti/Si/C
4 92 Ti/SiC/TiC
5 93 Ti/SiC/C
6 98 Ti/Si/C + Al
and I expect an output image like figure 4 (pag 6) of chapter 1 of this link:
https://books.google.es/books?id=zNWeBQAAQBAJ&printsec=frontcover&hl=es#v=onepage&q&f=false
But my output file does not have to do with this image.
Any idea / help?
Thanks in advance.
For your data, you don't need a histogram. Horizontal bars are sufficient. Instead of rotating a graph with plotting style with boxes you can use the plotting style with boxxyerror.
Make sure that your data separator is TAB or put your labels into "...".
### horizontal bars
reset session
set colorsequence classic
set datafile separator "\t"
$Data <<EOD
# index, purity, name
1 98 Ti/Si/TiC
2 94 Ti/TiSi_2/TiC
3 93.6 Ti/Si/C
4 92 Ti/SiC/TiC
5 93 Ti/SiC/C
6 98 Ti/Si/C + Al
EOD
unset key
set xlabel "Ti_2SiC_2 content, wt%" enhanced
set xrange [90:100]
set ylabel "Powder mixture"
unset ytics
set yrange [0:7]
set style fill solid 1.0
plot $Data u 2:1:(0):2:($1-0.4):($1+0.4):1 with boxxyerror lc variable,\
'' u 2:1:3 with labels offset 1,0 left
### end of code
Result:

How to change the color of a label in gnuplot [duplicate]

Very simple stuff. I want a function, say function f, to be plotted with a particular color. I also want a piece of label saying "function f color" which is also displayed in that color.
I am trying this so far:
set style line 1 lw 3 lc 1
set label "AC" at 0, 70 textcolor 1
but apparently the "lc" and "textcolor" follows different specs, and it complains:
"trolo4.pl", line 8: colorspec option not recognized
any help would be great!
Try this instead:
set label "AC" at 0, 70 textcolor linetype 1
or
set label "AC" at 0, 70 textcolor linespec 1
Read the manual in gnuplot> help label to learn more.
The answer by #holygeek works fine. Coming from python, I find that it is often nice to be a little more explicit.
set style line 1 lw 1 lc rgb "red"
set label "AC" at 0, 70 tc rgb "red"
Note that the set of color names recognized by your gnuplot is system dependent (see show colornames for a complete list). To achieve complete system independence, you can use the #RRGGBB version. e.g. red is '#ff0000, green is #00ff00 and blue is #0000ff. of course, you can make up all sorts of interesting colors (again, see show colornames for a list of pre-defined colors and their equivalent #.....)
for more info, also see help colorspec

Gnuplot - Labels cut off plot

My x and y labels are cut off the pic
I found the crop/nocrop option but didn't work.
How can I set a margin? and as you can see the titles (top right) are covered by the data. How can I set a margin there?
The following code comes from my bash script.
#set output
set terminal png large size 1920,1080 enhance background rgb '$BKGD_COLOR'
set output '$PLOT_OUTPUT_DIR/BW_${ArrayFile[$j]}_$DATE.png'
#set data
set datafile separator ","
set timefmt '%d/%m/%Y %H:%M:%S'
set xdata time
set format x "%d/%m/%Y\n%H:%M:%S"
#set axis (new style named 11, disable top and right axis, disable tics on top and right)
set style line 11 linecolor rgb '$TEXT_COLOR' linetype 1
set border 3 back linestyle 11
set tics nomirror font "/usr/share/fonts/dejavu/DejaVuSans-Bold.ttf,16" textcolor rgb "$TEXT_COLOR"
#set grid
set style line 12 linecolor rgb '$TEXT_COLOR' linetype 0 linewidth 1
set grid back ls 12
#set line style
set style line 1 lc rgb '$RCVD_COLOR' pt 1 ps 1 lt 1 lw 2
set style line 2 lc rgb '$SENT_COLOR' pt 6 ps 1 lt 1 lw 2
#set text
set key font "/usr/share/fonts/dejavu/DejaVuSans-Bold.ttf,10" textcolor rgb "$TEXT_COLOR"
set title 'Bandwidth (Mbps)' font "/usr/share/fonts/dejavu/DejaVuSans-Bold.ttf,14" textcolor rgb '$TEXT_COLOR'
#Removed - set ylabel 'Mbps' textcolor rgb '$TEXT_COLOR'
set yrange [0:*]
#plot using the column 1 and 3 of the CSV file. with line points and title 'Bytes Received' and line style 1 (as defined above)
plot '$DIR/ResultsCSV/mg_bandwidth/${ArrayFile[$j]}.csv' u 1:3 w lp ls 1 t 'Bytes Received', '$DIR/ResultsCSV/mg_bandwidth/${ArrayFile[$j]}.csv' u 1:4 w lp ls 2 t 'Bytes Sent'
Set the font-size of your tics when setting the terminal. This size is used to determine the automatic margins:
set terminal png ... font ',16'
Alternatively you can set explicit margins with
set lmargin screen 0.05
set bmargin ...
For possible coordinate types, see e.g. https://stackoverflow.com/a/23180595/2604213
BTW: Use the pngcairo terminal which has a much better rendering quality.
#Christoph provided the answer about the margin, but you asked about your key as well.
In order to fix that, you can put the key in a different position. Doing
set key inside top left
will move the key to the left side, where the data won't cover it up. You can also move it ouside the plot altogether with
set key outside top right
which will move it to the right side and outside of the plot where it won't be covered up.
See help set key for more detail.

How to find logical layout of tk widget?

I am learning ttk in depth and want to know how to find logical layout for any widget. Right now just exploreing and doing small experiments. In following code trying to show text but layout details are not documented for checkbutton.
So question arised for me how to find them for any widget easily.
This Ttk style guide is really nice reference but not helping in layout in depth.
Thanking you.
ttk::style element create pin vsapi EXPLORERBAR 3 {
{pressed !selected} 3
{active !selected} 2
{pressed selected} 6
{active selected} 5
{selected} 4
{} 1
}
ttk::style layout Explorer.Pin {Explorer.Pin.pin -sticky news}
pack [ttk::checkbutton .pin -style Explorer.Pin]
To find the style in use for a given widget use the winfo class command:
% ttk::checkbutton .b
% winfo class .b
TCheckbutton
You can then dump the layout using ttk::style layout: (reformatted for readability)
% ttk::style layout TCheckbutton
Checkbutton.padding -sticky nswe -children {
Checkbutton.indicator -side left -sticky {}
Checkbutton.focus -side left -sticky w -children {
Checkbutton.label -sticky nswe
}
}
This declares the elements and how they are to be placed. So to replace the indicator element you can copy this layout to define a new layout that references your new element:
% ttk::style layout Pin.TCheckbutton {
Checkbutton.padding -sticky nswe -children {
Checkbutton.pin -side left -sticky {}
Checkbutton.focus -side left -sticky w -children {
Checkbutton.label -sticky nswe
}
}
}
% place [ttk::checkbutton .pin -text text -style Pin.TCheckbutton] -x 10 -y 10
You should note that some of the elements pick up additional configuration attached to the style by the ttk::style configure command so when copying a style you should also copy the configuration:
ttk::style configure $new_stylename {*}[ttk::style configure $old_stylename]
and most likely also the map of widget states (ttk::style map).
Reading the ttk library files in <Tcl/Tk folder>/library/ttk should show quite a bit about how these things are put together. The vsapi.tcl file in particular does quite a bit of layout for Windows.

Resources