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

StackOverflow https://stackoverflow.com/questions/14741307

  •  07-03-2022
  •  | 
  •  

Question

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.

Was it helpful?

Solution

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top