0

This part of my code is the problem: I want to add names to Name_List by entering text into the entry widget. Then use the "add" button to insert the item into Name_List by the function add_name.

import tkinter as tk
from tkinter import *

Name_List = []

...

class NamePage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        name=StringVar()
        name_entry= tk.Entry(self, text="Enter name:", textvariable=name)
        name_entry.pack()
        add_button= tk.Button(self, text="Add",
                              command=lambda: self.add_name(name))
        add_button.pack()
        To_Role_List= tk.Button(self, text="Assign Roles",
                                command=lambda: self.print_namelist())
        To_Role_List.pack()

    def add_name(self,name,i=0):
        Name_List.insert(i, name)
        print(Name_List)
        i+=1

    def print_namelist(self):
        for i in [0,len(Name_List)]:
            print (Name_List[i])


app=TOS_outline()
app.mainloop()

My result, after entering the name "Alpha" into the Entry widget, appears to be this (using print of course):

[<tkinter.StringVar object at 0x03E7D650>]

What do I need to change/fix?

2
  • The indentation is broken in the code in your question Commented Jul 3, 2017 at 20:46
  • Hi Bryan, I just fixed it. Commented Jul 3, 2017 at 20:50

2 Answers 2

1

If you want to use a python list, dont use a counter or range like:

def add_name(self,name,i=0):
    Name_List.insert(i, name)
    print(Name_List)
    i+=1

def print_namelist(self):
    for i in [0,len(Name_List)]:
        print (Name_List[i])

instead use:

def add_name(self,name):
    Name_List.append(name)
    print(Name_List)

def print_namelist(self):
    for name in Name_List:
        print (name)

And use get() to obtain the value of the StringVar object

nombre = StringVar() 
id_art = IntVar()  
nombre.set("Python para impacientes") 
id_art.set(1)  
blog = ttk.Entry(ventana, textvariable=nombre, width=25) 
arti = ttk.Label(ventana, textvariable=id_art) 
print('Blog:', nombre.get()) 
print('Id artículo:', id_art.get())

Example from: http://python-para-impacientes.blogspot.com.es/2016/02/variables-de-control-en-tkinter.html (Spanish)

Sign up to request clarification or add additional context in comments.

4 Comments

Unfortunately, I get the same response: ['']
name was declared on __ init __ func and doesnt exists anymore out. Try add_button= tk.Button(self, text="Add", command=lambda: self.add_name(name.get()))
I still get a [''] when I print the list
Edit: I figured out the problem. You were right, but it was something else in my code (that was in the ...) causing the problem. I had app.Tk() and app.TOS_outline. After I deleted app.Tk() and moved app.TOS_outline to a different spot, I got the program to work
1

You are passing the variable to .insert instead of its value. Name_List.insert(i, name) must be Name_List.insert(i, name.get()).

1 Comment

I changed it. Now all I get is: ['']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.