python - Separating list items into columns with single label call -


i need separate list items columns. problem don't know how many items in list when it's created (i'm making list using nltk's tokenizer). how put item 1 in column 1, item 2 in column 2, etc. i've put in simplified code below. right puts things in correct amount of columns, last item in list in each column. thanks.

from tkinter import *  import ttk  root = tk() root.title("this space intentionally left blank") root.minsize(800,200) mainframe = ttk.frame(root)  mainframe.grid(column = 0, row = 0)  tokens = [0,1,2,3,4,5,6] numtok = len(tokens) tok = stringvar() x=0 while x < numtok:     tok.set(tokens[x])     ttk.label(mainframe, textvariable=tok).grid(column=x+1, row=2)     x += 1  root.mainloop() 

tkinter labels textvariable set update text if underlying stringvar changes.

you creating single stringvar updating each loop causing labels have same text.

try instead:

from tkinter import *  import ttk  root = tk() root.title("this space intentionally left blank") root.minsize(800,200) mainframe = ttk.frame(root)  mainframe.grid(column = 0, row = 0)  tokens = [0,1,2,3,4,5,6] tok_vars = [] token in tokens:     var = stringvar()     var.set(token)     tok_vars.append(var)  ii,var in enumerate(tok_vars):     ttk.label(mainframe, textvariable=tok_vars[x]).grid(column=ii+1, row=2)  root.mainloop() 

basically give different stringvarto each label.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -