user1661865
user1661865

Reputation: 75

Getting strings from TkInter entry widget

I'm a newbie in Python and I'm trying to create a GUI using TkInter. The problem I'm having is when I'm trying to display the entry text. Below are the two functions that I have made for this task. Is there something missing in my code?

This is my first function where I created the entry widgets:

def getArea():

  x1 = StringVar()
  x2 = StringVar()
  y1 = StringVar()
  y2 = StringVar()

  #coor x1
  labelX1 = Label(input, text="X: ").grid(row=1, column=1)
  entryX1 = Entry(input, width=8, textvariable=x1).grid(row=1, column=2)
  #coor x2
  labelY1 = Label(input, text="Y: ").grid(row=2, column=1)
  entryY1 = Entry(input, width=8, textvariable=y1).grid(row=2, column=2)
  #coor y1
  labelX2 = Label(input, text="X: ").grid(row=1, column=3)
  entryX2 = Entry(input, width=8, textvariable=x2).grid(row=1, column=4)
  #coor y2
  labelY2 = Label(input, text="Y: ").grid(row=2, column=3)
  entryY2 = Entry(input, width=8, textvariable=y2).grid(row=2, column=4)

  x1.set("Defalut value x1")
  x2.set("Defalut value x2")
  y1.set("Defalut value y1")
  y2.set("Defalut value y2")

  coorx1 = x1.get()
  coorx2 = x2.get()
  coory1 = y1.get()
  coory2 = y2.get()

  button = Button(input, text='ok',command=lambda:  showResults(coorx1,coorx2,coory1,coory2)).grid(row=1, column=5)

  exitButton = Button(input, text='exit', command=input.destroy).grid(row=2,column=5)

  input.mainloop()

This is my second function where I want to display the strings:

def showResults(x1,x2,y1,y2):
showInfo = Tk()
showInfo.title("Location Temperature")
showInfo.geometry("270x100+470+320")

print x1, x2, y1, y2

info1 = Label(showInfo, text=x1).pack()
info2 = Label(showInfo, text=x2).pack()
info3 = Label(showInfo, text=y1).pack()
info4 = Label(showInfo, text=y2).pack()

buttonClose = Button(showInfo, text='exit', command=showInfo.destroy).pack(side= RIGHT)
showInfo.mainloop()

Upvotes: 1

Views: 9317

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385900

There are many things that need fixing with your code. The first of which is you're creating two instances of Tk. Tkinter is designed such that you should only ever create one instance of that class, and call the mainloop of that instance exactly once. If you need a second window, create an instance of Toplevel.

Second, you don't need to use StringVars. I know a lot of examples on the internet use them, but strictly speaking you don't need them. As a rule of thumb, you only need those if you are going to set a trace on them or if you are going to have two widgets share the same variable (which is actually a pretty cool feature). Instead, you can call the get method of a text widget to get the value. The main advantage to this is simply that you have fewer objects that you need to create and manage.

So, what you could do is one of the following:

  1. you can save a reference to each of the entry widgets in either a global variable or an instance variable (if you switch to using an object oriented style of programming),
  2. you can pass in the reference to the widgets to your showResults function.
  3. you can continue to use StringVars, and have both the entry widget and the label in your showResults function share the same StringVars. Again, you'll need to either store those in a way that the two functions can use them, or pass them from one function to the other.

For an example of the second -- passing references and using the get method -- you would do something like this:

button = Button(input, text='ok',command=lambda:  x1=entryX1, y1=entryY1, 
    x2=entryX2, y2=entryY2: showResults(x1,y1,x2,y2)
...
def showResults(x1,y1,x2,y2):
    info1 = Label(showInfo, text=x1.get())
    info2 = Label(showInfo, text=y1.get())

This isn't the only way, or even the best way. The point is, there's no mystery here. Like with variables used by more than one function, you either need to keep a global reference somewhere, or you need to pass a reference around. Or, you leverage the special properties of the textvariable attribute.

Upvotes: 3

Related Questions