How to create a status line with ttk? - r

How do you create your status line at the bottom of your window? An inactive entry does not look very nice. What other options are there?
Is possible to integrate a progress bar which is visible only on demand?
(I am using tk and ttk from within R.)
EDIT: Now here is my second version, which works fine for me, but I would like to display the whole status bar on demand only (similar to what the status bar in the Chrome browser does). How can I do that? Calling tklower(f) does not help...
library(tcltk)
library(tcltk2)
tkdestroy(root)
root <- tktoplevel()
status <- tclVar("")
progress <- tclVar("0")
b <- tk2button(root, text="fake doing something!")
tkpack(b, padx=40, pady=10)
o <- tk2checkbutton(root, text="show progress", variable=progress)
tkpack(o, pady=10)
f <- tk2frame(root, relief="sunken")
l <- tk2label(f, textvariable=status)
tkpack(l, side="left", pady=2, padx=5, expand=0, fill="x")
tkpack(f, side="left", expand=1, fill="x", anchor="s")
sg <- ttksizegrip(root)
tkpack(sg, side="left", expand=0, anchor="se")
doit <- function() {
tclvalue(status) <- "working (hard) ..."
tcl("update")
do.pb <- tclvalue(progress)=="1"
if(do.pb) {
pb <- tk2progress(f, length=60, mode="determinate")
tkpack(pb, side="right", expand=0, padx=3, pady=2)
tkconfigure(pb, maximum=100, value=0)
}
for(i in 1:100) {
if(do.pb) {
tkconfigure(pb, value=i)
tcl("update")
}
Sys.sleep(0.03)
}
if(do.pb) tkdestroy(pb)
tclvalue(status) <- "Ready."
}
tkconfigure(b, command=doit)
tclvalue(status) <- "Ready."

I use a ttk::frame widget. In that I'll place one or more ttk::label widgets, and a ttk::sizegrip widget on the far right.
As for the progress bar -- just add it as usual. If you use grid, you can use grid remove to remove it from the statusbar but grid will remember its settings so you can add it right back. Or, you can user lower and raise to remove it from view and bring it back again.
Your question about the sizegrip widget is unclear. What about it doesn't work for you on windows?

By status line do you mean like the status bar you find at the bottom of most browser windows? If so, I usually use a label with its textvariable linked to a variable containing the status string.

Related

'progress' progress bar re-prints every iteration

When I use the package 'progress' like so:
library(progress)
pb <- progress_bar$new(total = 100)
for(i in 1:10){
pb$tick()
}
I get a repeated output like so:
[==>----------------------------------------------------------------------------------------------] 3%[===>---------------------------------------------------------------------------------------------] 4%[====>--------------------------------------------------------------------------------------------] 5%[=====>-------------------------------------------------------------------------------------------] 6%[======>------------------------------------------------------------------------------------------] 7%[=======>-----------------------------------------------------------------------------------------] 8%[========>----------------------------------------------------------------------------------------] 9%[=========>---------------------------------------------------------------------------------------] 10%[==========>--------------------------------------------------------------------------------------] 11%[===========>-------------------------------------------------------------------------------------] 12%...
I would prefer it to delete the old bar after the new one is called. Is there a setting in R studio that I need to change to use that? I'm using a mac if that matters.

R & gwidgets2 - Collecting values from gRadio widget

My problem is very basic (I am a beginner user in R). I am trying to collect the value selected from a gradio widget (gwidgets2 package for R).
I am using a similar script as this simplified one :
U=vector(mode="character")
DF=function() {
Win=gbasicdialog(handler=function(h,...) {
T=svalue(A)
print(T)
# I can print but not assign the value using : assign (U,T, .GlobalEnv)
})
A<-gradio(c("1","2","3"), selected=1,container=Win,)
out <- visible(Win)
}
DF()
Using this script, I am able to print the value selected in the gradio widget, but when I try to assign this value to another variable passed to the global environment, I get an error.
It is strange as this structure of script works fine to collect values from other widgets (like gtable). What am I doing wrong ?
Thanks for the help.
I am not sure what goes wrong, but was able to run your code with a small change:
DF <- function() {
Win <- gbasicdialog(
handler = function(h, ...) {
.GlobalEnv$varT = svalue(A)
print(varT)
}
)
A <- gradio(c("1", "2", "3"), selected = 1, container = Win)
out <- visible(Win)
}
DF()
A small advice: avoid using the single letters T or F, as in your code T might be interpreted as TRUE and not object T.

R : How to control the right click of mouse in gWidgets

In gWidget GUI I have seen a feature on right click on mouse
Copy
Save
How can I use that Save handler so save my shown dataset in table ?
Kindly refer to the image below :
Also in my code I have used graphicspane1 <- ggraphics(cont = group1)
Kindly suggest answers ASAP
Here is a sample code,
library(cairoDevice)
library(gWidgets)
library(gWidgetsRGtk2)
require(RGtk2)
fun <- function(){
tbl <- data.frame(A=c(1,2,3),X=c('A','B','C'))
grid.draw(tableGrob(tbl, gp=gpar(fontsize=8, lwd=1.2)))
}
options(guiToolkit = "RGtk2")
w <-gwindow("GUI")
g <-ggroup(cont=w)
plotbutton4 <- gbutton('Draw Table', cont = g, handler=function(h,...){
fun()})
graphic1 <- ggraphics(cont=g)
Steps : 1. Click on the button "Draw Table"
Step : 2. Right Click and try to save the data.
How can I make a relation with that Save & Copy Button, more focus on SAVE

How do I update a gedit box instantly after using gfile to specify a filepath, using gWidgetsRGtk2, in R

I am trying to create a GUI using gWidgetsRGtk2 for a program that I have written in R. My GUI has a gedit() text box in which the user can type a file path for the input data file to be put into the program. It also has a 'browse' button, which, when clicked, opens up a gfile() box so that they can browse for the file that they want. What I am having trouble with is updating the value in my gedit() box, after the user has selected their file using the 'browse' button. The code below may make this clearer:
dir <- getwd()
sfilepath <- paste0(dir,"/")
win = gwindow("Set Parameters:",width=400,height=550)
nb = gnotebook(cont=win)
tab2 <- glayout(cont=nb, label = "Advanced Settings")
tab1 <- glayout(cont=nb, label = "Basic Settings")
tab1[2,2] <- glabel("BD:",cont=tab1)
tab1[2,4:5] <- gedit(1,cont=tab1)
addhandlerkeystroke(tab1[2,4],handler=function(h,...){BD <<- as.numeric(svalue(h$obj))})
tab1[3,2:5] <- gseparator(cont=tab1)
tab1[4,2:5] <- glabel("File path:",cont=tab1)
tab1[5,2:4] <- gedit(paste0(dir,"/"),cont=tab1)
tab1[5,5] <- gbutton(text="Browse", handler=function(h,...){ gfile("Select a file",type="open", filter = list("text files" = list(patterns = c("*.csv","*.txt")), "R files" =list(patterns = c("*.R","*.Rdata"))), handler = function(h,...){ sfilepath <<- h$file},cont=TRUE)},cont=tab1)
addhandlermousemotion(tab1[5,2],handler=function(h,...){svalue(h$obj) <- sfilepath})
So far I have tried using addhandlermousemotion, as in the code above, so the text in the gedit() box is only updated when you move the mouse over the box itself. However, I would prefer it if the text in the box updated instantly.
I have also tried using addhandleridle(), with an interval of 1 second, so that the text in the box will be automatically updated every 1 second. This worked. However, it made it impossible to type in the box properly, because the text box was being updated with the old 'sfilepath' before it had saved the new 'sfilepath' that was being typed in.
I am a beginner at making at GUIs (I have written a program for work, but it needs to be used by someone else once I leave, so decided last Friday that I should figure out how to make it into a GUI). So any help that anyone can offer would be greatly appreciated.
Here is the pattern you want (passing a handler to gfilebrowse):
w <- gwindow("test")
g <- ggroup(cont=w, horizontal=FALSE)
file_upload <- gfilebrowse(cont=g, handler=function(h,...) {
svalue(e) <- svalue(h$obj)
})
e <- gedit("", cont=g)

Adding a GIF image to a TclTk window

I wish to insert a 'loading' GIF image to my tcltk window but just can't get my head around it. Following is a reproducible example:-
backg <- 'white'
pdlg <- tktoplevel(background=backg)
tcl('wm', 'geometry', pdlg, '500x100+450+350')
tilg <- 'Package installation in progress'
tkwm.title(pdlg, tilg)
fn <- tkfont.create(family = 'helvetica', size = 12)
nwlabel <- " The requisite packages are being installed. This may take several \nminutes... \n"
tllab <- tklabel(pdlg, text = nwlabel, font = fn, pady = 0, background=backg)
clickEv <- tclVar(0)
OK.but <- tkbutton(pdlg, text=' Stop ', command=function() tclvalue(clickEv) <- 1, font=fn, background='grey', pady=0)
tkgrid(tllab, columnspan=1)
tkgrid(OK.but, row=3)
tkbind(pdlg, "<Destroy>", function() tclvalue(done) <- 2)
tkfocus(pdlg)
#This allows me to insert a GIF image but the animation is lost. Also it would be convenient if the output can be obtained using 'tkgrid' instead of 'tkpack'
pdlg2 <- tktoplevel(background='white')
loading <- tclVar()
tcl('image', 'create', 'photo', loading,
file='file path to GIF file')
trial <- tklabel(pdlg2, image=loading)
tkpack(trial)
An example GIF file can be downloaded from here -http://www.dlfpramericalife.com/library/images/final_loading_big.gif
Ideally, the GIF image should be placed above the 'Stop' button but below the text. Many thanks for your help!
In Tcl/Tk, it's quite easy.
set img [image create photo -file nameofthefile.gif]
label .l -image $img
I don't know R, but taking your code as a guide, I imagine something like this, but please check it!
img <- tkimage.create('photo', file='nameofthefile.gif')
imglab <- tklabel(pdlg, image = img)
... then you grid/pack/place it wherever you want. Please note that this don't work with animated gifs and I think animation must be hand-handler, using a timer which updates periodically the image content, but I never did it, nor I know how to do. You may check the Tcl/Tk wiki for more help.

Resources