Display dictionary contents in a label in python tkinter
<p>Ive been trying for a while now but i cant seem to display the contents of a dictionary in a label. I want to hit the display button and when i do, i want all the contents in my dictionary to display, see the code below:</p>

<pre><code> import sys
from tkinter import *
from tkinter import ttk
import time
from datetime import datetime
now= datetime.now()
x = []
d = dict()
def quit():
    print("Have a great day! Goodbye :)")
    sys.exit(0)
def display():
    for key in d.keys():
        x.append(key)
def add(*args): 
    global stock
    global d
    stock = stock_Entry.get()
    Quantity = Quantity_Entry.get()
    if stock not in d:
        d[stock] = Quantity
    else:
        d[stock] += Quantity




root = Tk()
root.title("Homework 5 216020088")

mainframe = ttk.Frame(root, padding="6 6 20 20")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))

ttk.Label(mainframe, text="you are accesing this on day %s of month %s of %s" % (now.day,now.month,now.year)+" at exactly %s:%s:%s" % (now.hour,now.minute,now.second), foreground="yellow", background="Black").grid(column=0, row = 0)

stock_Entry= ttk.Entry(mainframe, width = 60, textvariable="stock")
stock_Entry.grid(column=0, row = 1, sticky=W)
ttk.Label(mainframe, text="Please enter the stock name").grid(column=1, row = 1, sticky=(N, W, E, S))

Quantity_Entry= ttk.Entry(mainframe, width = 60, textvariable="Quantity")
Quantity_Entry.grid(column=0, row = 2, sticky=W)
ttk.Label(mainframe, text="Please enter the quantity").grid(column=1, row = 2, sticky=(N, W, E, S))

ttk.Button(mainframe, text="Add", command=add).grid(column=0, row=3, sticky=W)
ttk.Button(mainframe, text="Display", command=display).grid(column=0, row=3, sticky=S)
ttk.Button(mainframe, text="Exit", command=quit).grid(column=0, row=3, sticky=E)

ttk.Label(mainframe, text= x).grid(column=0, row= 4, sticky=W)

for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
root.mainloop()
</code></pre>

<p><code>Label(..., text= x)</code> will display only once - at the start.</p>

<p>You have to use <code>StringVar</code> to do what you expect</p>

<pre><code>x_var = StringVar()
Label(..., texvariable=x_var)
</code></pre>

<p>Now you can set value in <code>x_var</code> to automatically set new text in <code>Label</code></p>

<pre><code>x_var.set( x )
</code></pre>

<hr>

<p>BTW: instead of</p>

<pre><code>for key in d.keys():
    x.append(key)
</code></pre>

<p>probably you should do</p>

<pre><code>x = list(d.keys())
</code></pre>

<p>or maybe even</p>

<pre><code>x = list(d)
</code></pre>

<hr>

<p><strong>EDIT:</strong> display all keys (as in your code) when you press button "Display"</p>

<pre><code>import sys
from tkinter import *
from tkinter import ttk
import time
from datetime import datetime

# --- functions ----

def quit():
    print("Have a great day! Goodbye :)")
    sys.exit(0)

def display():
    x_var.set( list(d) )

def add(*args): 
    global stock
    global d
    stock = stock_Entry.get()
    Quantity = Quantity_Entry.get()
    if stock not in d:
        d[stock] = Quantity
    else:
        d[stock] += Quantity

# --- main ---

x = list()
d = dict()

now = datetime.now()

root = Tk()
root.title("Homework 5 216020088")

x_var = StringVar()

mainframe = ttk.Frame(root, padding="6 6 20 20")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))

ttk.Label(mainframe, text="you are accesing this on day %s of month %s of %s" % (now.day,now.month,now.year)+" at exactly %s:%s:%s" % (now.hour,now.minute,now.second), foreground="yellow", background="Black").grid(column=0, row = 0)

stock_Entry= ttk.Entry(mainframe, width = 60, textvariable="stock")
stock_Entry.grid(column=0, row = 1, sticky=W)
ttk.Label(mainframe, text="Please enter the stock name").grid(column=1, row = 1, sticky=(N, W, E, S))

Quantity_Entry= ttk.Entry(mainframe, width = 60, textvariable="Quantity")
Quantity_Entry.grid(column=0, row = 2, sticky=W)
ttk.Label(mainframe, text="Please enter the quantity").grid(column=1, row = 2, sticky=(N, W, E, S))

ttk.Button(mainframe, text="Add", command=add).grid(column=0, row=3, sticky=W)
ttk.Button(mainframe, text="Display", command=display).grid(column=0, row=3, sticky=S)
ttk.Button(mainframe, text="Exit", command=quit).grid(column=0, row=3, sticky=E)
ttk.Label(mainframe, textvariable=x_var).grid(column=0, row= 4, sticky=W)

for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
root.mainloop()
</code></pre>

python user-interface dictionary tkinter