简体   繁体   中英

Filename in text box using tcltk in R

Using tcltk to create a GUI in R, I want to make a non-editable text box that displays the name of the save file that was selected by the user. I am able to create the button and the box, but I cannot figure out how to display the name of the selected file. I think I need to use tkinsert()

This is what I have so far:

library(tcltk)
library(tcltk2)
library(readxl)

test1 <- tktoplevel()
tkwm.title(test1, "Test 1")

tkgrid.rowconfigure(test1, 4)
tkgrid.columnconfigure(test1, 3)

getXlsx <- function() {
xlsheet <- tclvalue(tkgetOpenFile(
filetypes = "{ {Excel Files} {.xlsx} } { {All Files} * }"))
a <- read_excel(xlsheet)
assign("a", a, envir = .GlobalEnv)
}

test1$env$butSelect1 <- tk2button(test1, text = " Select File ", command 
= getXlsx)
tkgrid(test1$env$butSelect1, padx = c(10,0), pady = 10, column = 0, row = 
0)

test1$env$txt1 <- tk2text(test1, width = 40, height = 1)
tkgrid(test1$env$txt1, padx = c(10,10), pady = 10, column = 1, row = 0, 
columnspan = 2)
tkconfigure(test1$env$txt1, state = "disabled")
###  tkinsert(test1$env$txt1, ???)  ###

Any help would be greatly appreciated, thank you.

I got it now, I had to add 4 lines to the function itself:

getXlsx <- function() {
  xlsheet <- tclvalue(tkgetOpenFile(
    filetypes = "{ {Excel Files} {.xlsx} } { {All Files} * }"))
  a <- read_excel(xlsheet)
  assign("a", a, envir = .GlobalEnv)
  assign("z", xlsheet, envir = .GlobalEnv)
  tkconfigure(test1$env$txt1, state = "normal")
  tkinsert(test1$env$txt1, "end", z)
  tkconfigure(test1$env$txt1, state = "disabled")
}

This creates a new "z" value in the global environment with the path name, then allows the text box to be editable, then adds the path name, then makes the text box non-editable again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM