0

I am trying to create a GUI to receive text input from a user to define the number of cores to be used by a script. I am using the Entry function, but would like to label this since there will be other text entries required. I have the following code, which worked for the OptionMenu function, but I am not sure how to adapt it so that Entry can receive the textvariable option. I have written this as a function so it is possible to call it multiple times for different variables.

from Tkinter import *

root = Tk()

def UserInput(status,name):
  optionFrame = Frame(root)
  optionLabel = Label(optionFrame)
  optionLabel["text"] = name
  optionLabel.pack(side=LEFT)
  var = StringVar(root)
  var.set(status)
  w = apply(Entry, (optionFrame, textvariable= var))
  w.pack(side = LEFT)
  optionFrame.pack()

cores = UserInput("1", "Number of cores to use for processing")

root.mainloop()
218
  • 1,754
  • 7
  • 27
  • 38
  • BTW, right now, your function does not return anything. Either, you should return the string variable, or wait until the frame is closed and then return the value given by the user. Take a look at [this related question](http://stackoverflow.com/q/10057672/1639625). – tobias_k Aug 12 '14 at 17:58

1 Answers1

2

If you want to pass keyword arguments to apply, you have to use this syntax:

w = apply(Entry, [optionFrame], {"textvariable": var})

But instead you should just do it without apply and call the Entry constructor directly:

w = Entry(optionFrame, textvariable=var)

The Python documentation has this to say about apply:

Deprecated since version 2.3: Use function(*args, **keywords) instead of apply(function, args, keywords)

tobias_k
  • 81,265
  • 12
  • 120
  • 179