0

I am trying to create a GUI for a small plotting program of logging files. I have all the data in a dictionary(actually a dict{dict{dict{dict{}}}}})and would like to use the keys to create tab-quantities for top keys checkboxes that basically will be selected to plot the quantity versus time for the subkeys. I have looked into pyttk and it looks similar to what I wanted, though I am running into issues how to implement the tab/button creation.

Thanks a bunch in advance.

madtowneast
  • 2,350
  • 3
  • 22
  • 31
  • What's the question you are asking? Are you asking us to write the code for you? What issues are you having? – Bryan Oakley Oct 28 '11 at 16:43
  • No, I am just asking for an idea how to do it. I have tried to make buttons/checkboxes using a for loop over the keys, but that is like creating variables on the fly, which I dont like at all. I am just looking for different ideas cause I cant have not found any other solution. And making them all by hand is about as flexible as a brick wall and that is something I cant have. – madtowneast Oct 28 '11 at 16:58

1 Answers1

1

One way is to store references to your associated variables in a dictionary. Here's an example:

import Tkinter as tk

data = {"Field 1": 1,
        "Field 2": 2,
        "Field 3": 3,
        "Field 4": 4,
}

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.variables ={}
        for label in sorted(data.keys()):
            self.variables[label] = tk.IntVar()
            cb = tk.Checkbutton(self, text=label, 
                                onvalue=data[label], offvalue=0, 
                                variable=self.variables[label])
            cb.pack(side="top", fill="x")


        button = tk.Button(self, text="Submit", command=self.OnSubmit)
        button.pack()

    def OnSubmit(self):
        for field in sorted(data.keys()):
            print "Value for %s: %s" % (field, self.variables[field].get())

if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

If you don't like creating variables on the fly like that, with a little extra effort you can create an array that all of the buttons can be associated with. I gave an example of how to do it in the question How to run a code whenever a Tkinter widget value changes?

Community
  • 1
  • 1
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Thanks a lot! Very close to what I was thinking about, but i tried using a list instead of an dict. I got one quick question, why do I have to pass tk.TK into the class? – madtowneast Oct 28 '11 at 19:57
  • Mhh i was a bit too quick with that comment. I am guessing it comes from the fact that we have def __init__(self, *args, **kwargs) instead of def__init(self, master). – madtowneast Oct 28 '11 at 20:14
  • You aren't "passing" tk.Tk, that class is a subclass of tk.Tk. – Bryan Oakley Oct 28 '11 at 21:54