Python: Tkinter Entry widget is not working

4.5k views Asked by At
from Tkinter import *
root = Tk()
ticker1 = StringVar()
ticker2 = StringVar()
root.title("Choose Companies")
Label(root,
          text = "Company No. 1",
          fg = "black",
          font ="Helvetica 16 italic").pack()
name = Entry(root,name = ticker1 ).pack()
Label(root,
          text = "Company No. 2",
          fg = "black",
          font ="Helvetica 16 italic").pack()
name1 = Entry(root,name1 = ticker2).pack()
root.mainloop()

The code does not work it gives me this error:

exceptions.TypeError: cannot concatenate 'str' and 'instance' objects

The format to get an entry widget in the python GUI looks correct.
I'm using python 2.7 windows 8.1

2

There are 2 answers

1
uname01 On

The Entry class constructor has no keyword parameter named name1, you can pass a StringVar instance to the textvariable keyword parameter and retrieve text by calling the get() method on that StringVar instance. Alternately, you can just call the get() method on the Entry instance itself.

Example:

var = StringVar()
entry = Entry(root, textvariable=var)
entry.pack()

var.set('default value')
input = var.get()
0
Adam On
    from Tkinter import *
root = Tk()
ticker1 = StringVar()
ticker2 = StringVar()
root.title("Choose Companies")
Label(root,
          text = "Company No. 1",
          fg = "black",
          font ="Helvetica 16 italic").pack()
Entry(root,textvariable = ticker1 ).pack()
Label(root,
          text = "Company No. 2",
          fg = "black",
          font ="Helvetica 16 italic").pack()
Entry(root,textvariable = ticker2).pack()
root.mainloop()

Dude it was bad idea to assigne the entries to the name and name1. Instead you needed to add the keyword argument called 'textvariable' which is solution to your problem.