Frage

I'm building my first GUI and I want it to take basic numerical input from Spin Buttons and text boxes and populate a dataframe with these values, then plug them in to some function at the push of a button at the very end. I'm having trouble figuring out the Connection part of it however. I've got the window to look exactly how I want it to and I have all of my Spin Buttons calibrated to have the desirable default value and increase by the right number of increments, but then I'm having trouble passing it to my model. I have pre-allocated a dataframe and then I want to assign specific values to each element in the dataframe and name the columns using the input from a text box. I've tried simply assigning them with no success so far. I've also tried making them global variables and then putting them into a dataframe at the end, but no luck there either. I'm certain that I'm missing something in the gSignalConnect() command, but I can't figure it out. The code for one text box and one Spin Button is given below, as well as the code for my "Calculate" button (I have substituted in colMeans() for my function for the sake of space).

df <- data.frame(matrix(nrow=8,ncol=5))
library(RGtk2)
window <- gtkWindow()
window["title"]<- "My First GUI"

# Add a frame
frame <- gtkFrameNew("Input")
window$add(frame)

# Create vertical container for file name entry
vbox<- gtkVBoxNew(FALSE, 9)
frame$add(vbox)

# Add horizontal container for every widget line
hbox<-gtkHBoxNew(FALSE, 4)
vbox$packStart(hbox, FALSE, TRUE)
label <- gtkLabelNewWithMnemonic("Dataframe Column 1          ")
hbox$packStart(label,FALSE,TRUE)

# Add a text box that allows the user to name Dataframe Column1
col1nameLAB<-gtkLabel('Name: ')
col1name<-gtkEntryNew()
col1name$setWidthChars(20)
hbox$PackStart(col1nameLAB)
hbox$PackStart(col1name,FALSE,TRUE)
gSignalConnect (col1name,"value-changed",function (entry) {
names(df)[1] <- (entry$getText()}))

# Add a spin button for a numerical value that will eventually be the first value in Column 1 of the dataframe
col1valLAB <- gtkLabel("Integer Value: ")
col1valADJ <- gtkAdjustment(30,1,100,1)
col1valSB <- gtkSpinButton(col1valADJ,1,0)
gtkSpinButtonSetValue(col1valSB, 30)
hbox$PackStart(col1valLAB)
hbox$PackStart(col1valSB)
gSignalConnect (col1valSB,"value-changed",function (entry) {
df[1,1]<-entry$getValue()})

# Add a button to recalculate the model
hbox = gtkHBoxNew(FALSE,1)
vbox$packStart(hbox, FALSE, FALSE, 0)
button <- gtkButtonNew ( )
button$setLabel ( "Calculate" )
hbox$ packStart ( button )

# Now specify what happens when button is clicked:
gSignalConnect (button, "clicked",
f<-function( widget) {
  colmeans(df)
} )
War es hilfreich?

Lösung

This works, not sure what isn't working for you:

library(RGtk2)

df <- data.frame(a=1)

w <- gtkWindow()

col1valADJ <- gtkAdjustment(30,1,100,1)
col1valSB <- gtkSpinButton(col1valADJ,1,0)
w$add(col1valSB)

gSignalConnect(col1valSB, "value-changed", function(entry, ...) {
  val <- entry$getValue()
  message("Change to ", val)
  names(df)[1] <<- val
})
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top