Jalcock501
Jalcock501

Reputation: 399

ValueError: too many values to unpack Tkinter Listbox

When writing a simple program to output the values and keys in a dictionary to a listbox in python using Tkinter I get the following error:

    for key, value in mydict.itervalues():
ValueError: too many values to unpack

Please see my code below:

#!/usr/bin/python

import Tkinter as tk


mydict = {"0x00063":"6F 7D 9E 0E FF FF FF FF", 
          "0x00061":"FF FF FF FF FF FF FF FF", 
          "0x00062":"AA AA AA AA AA AA AA AA", 
          "0x00064":"00 00 00 00 00 00 00 00"}

guiMain = tk.Tk()
recvDisplay = tk.Listbox(guiMain)

for key, value in mydict.itervalues():
    recvDisplay.insert(key, value)

recvButton = tk.Button(guiMain, text="Start Receive")
tranDisplay = tk.Listbox(guiMain)
tranButton = tk.Button(guiMain, text="Start Transmit")
recvDisplay.pack()
recvButton.pack()
tranDisplay.pack()
tranButton.pack()
guiMain.mainloop()

I need to be able to output the values and keys within to dictionary to the listbox recvDisplay, but I need the listbox to automatically refresh so it displays any changes to the dict.

Upvotes: 0

Views: 363

Answers (2)

unutbu
unutbu

Reputation: 879291

mydict.itervalues() is an iterator over the values in mydict. If you want both the keys and the values, use mydict.iteritems():

for key, value in mydict.iteritems():
    recvDisplay.insert(tk.END, '{}, {}'.format(key, value))

Note: A Listbox only has one column. If you want multiple columns to display the key and value separately, you will need a different widget, such as a treectrl.MultiListbox, or put two Listboxes side-by-side.

Upvotes: 3

Vivek Sable
Vivek Sable

Reputation: 10213

  1. itervalues(): Return an iterator over the dictionary’s values.

e.g.

>>> d = {"a":1, "b":2}
>>> d.itervalues()
<dictionary-valueiterator object at 0xb7201144>
>>> for i in d.itervalues():
...   print i, type(i)
... 
1 <type 'int'>
2 <type 'int'>
>>> 
  1. items(): Return a copy of the dictionary’s list of (key, value) pairs.
  2. List item

e.g.

>>> d.items()
[('a', 1), ('b', 2)]
  1. iteritems(): Return an iterator over the dictionary’s (key, value) pairs.

e.g.

>>> for i, j in d.iteritems():
...   i , j
... 
('a', 1)
('b', 2)
>>> 

more details

Upvotes: 1

Related Questions