1

I'm learning Tkinter and have two questions.

[1st one answered in the comment section]

1 - Why do the items in the dictionary are not printed in the correct order? If you delete the "grid" part from the Button, you may see that they are not ordered.

2 - Why the button is not shown? I want to make a button just below the checkboxes, which will print out "1" for each clicked checkboxes.

This link was my reference.

Below is my code:

from Tkinter import *

master = Tk()

def read():
    print dict[mini].get()

dict = {"first": 0, "second": 0, "third": 0}


for mini in dict:
    dict[mini] = Variable()
    l = Checkbutton(master, text=mini, variable=dict[mini])
    l.grid(sticky=W, pady=4)
    l.pack()

Button(master, text='Show', command=read).grid(row=len(dict) + 1, sticky=W, pady=4)

master.mainloop()

Edit: Spelling

Community
  • 1
  • 1
firko
  • 289
  • 2
  • 10
  • 1
    Dictionaries do not keep their order. You may want to take a look at [`collections.OrderedDict`](https://docs.python.org/2/library/collections.html#collections.OrderedDict). – zondo Apr 09 '16 at 12:03
  • Didn't know that, thanks for the help. However, I couldn't seem to make it work. What did I do wrong here? http://pastebin.com/t7yMrCwu – firko Apr 09 '16 at 12:20
  • 1
    Passing it a dictionary, the dictionary will be unordered and `OrderedDict` won't know what the original order was. You should use `OrderedDict([('first', 0), ('second', 0)])`. – zondo Apr 09 '16 at 12:23
  • The button does not show because the program errors out at the pack statement. Remove the pack() and the button shows. You can not mix grid and pack in the same container. Also the function named Variable() has not been declared so that may be your next error, and the dictionary of Variable()s is never changed. Finally, do not use "i", "l", or "o" as single character variable names as it can be difficult to tell which is a letter and which is a number. –  Apr 09 '16 at 18:26

1 Answers1

0

The button does not show because the program errors out at the pack statement. Remove the pack() and the button shows. You can not mix grid and pack in the same container. (from what Curly Joe said in the comments).

The Tkinter variable declared in my_dict are not initialized nor assigned and therefore cannot be used; the dictionary of Variable()s is never changed.

from Tkinter import *
from collections import OrderedDict

master = Tk()

def read():
    try:
        print my_dict[mini]
    except KeyError:
        pass

my_dict = OrderedDict()
my_dict["first"] = 0
my_dict["second"] = 0
my_dict["third"] = 0


for mini in my_dict.keys():
    my_dict[mini] = Variable()
    chkbtn = Checkbutton(master, text=mini, variable=my_dict[mini])
    chkbtn.grid(sticky=W, pady=4)

Button(master, text='Show', command=read).grid(row=len(my_dict) + 1, sticky=W, pady=4)

master.mainloop()

Finally, do not use i, l, or o as single character variable names as it can be difficult to tell which is a letter and which is a number; also do not use the name of builtins for your variables or method names.

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80