I have the following code where I have several small problems.

I have included some comments as I can't seem to get my 'pop up menu' button to work in order to retrieve the number of columns requested by the user.

I then need to be able to get the values entered into each entry box within these columns but by EACH ROW (a list of values per row) and I have not got a clue how to get these values when I don't always know how many columns I will have (see lambda function where there is currently 4 'set' values but need to have 'n').

If anyone could help as I'm really reaching a dead here, I would be eternally grateful! I'm fairly new to python and especially Tkinter and therefore it may be a problem within my class structure as this is my first program written using class and self.

def ValueBox(self):
    self.number_boxes = self.value.get()
    self.win2.destroy
    return self.number_boxes

def ChoiceBox(self, choice):
    if choice == "List":
        self.win2 = Tk()
        self.win2.title("List")
        self.list_text = Label(self.win2, text="Please enter number of values to be used:")
        self.list_text.grid(row=0, column=0, sticky="nsew", padx=1, pady=1)
        self.value = StringVar()
        self.list_values = Entry(self.win2, textvariable=self.value, justify="center")
        self.list_values.grid(row=1, column=1, sticky="nsew", padx=1, pady=1)
        #self.value_number = self.value.get()

        #self.list_button = ttk.Button(self.win2, text="Enter", command=self.ValueBox)
        self.list_values.bind('<Return>', self.ValueBox) # Link entry button to box.
        #self.list_button.grid(row=2, column=1, sticky="nsew", padx=1, pady=1)
        self.win2.mainloop()

        #self.number_boxes = 4
        column = 7

        self.numbers = [StringVar() for i in xrange(self.number_boxes) ] #Name available in global scope. 

        for i in xrange(self.number_boxes): 
            choice_title = Label(self.frame_table, text='Value %g'% float(i+1), bg='white', borderwidth=0, width=10) 
            choice_title.grid(row=1, column=column+i, sticky="nsew", padx=1, pady=1) 
            boxes=[]

        for i in xrange(self.number_boxes):
            for j in range(2, rows): 
                box=Entry(self.frame_table,bg='white',borderwidth=0,textvariable=self.numbers[i], width=10, justify="center") 
                box.grid(row=j,column=column+i, sticky='nsew', padx=1, pady=1) 
                boxes.append(box)
                print boxes
                # = map(float, co_ord.strip('()').split(','))
        #box1,box2,box3,box4=boxes


    for i in self.numbers: 
        i.trace('w',lambda a,b,c,n=i: self.numberwritten(n) ) 




    def numberwritten(self, numbers):
    fg = self.numbers.get()
    print fg

You should post complete functional class not part snipped, which is not even
correctly indented.

you should also show how you are instantiating the class and the Tkinter, as you are unsure if you are doing it correctly.

Edited 4 Years Ago by pyTony

I accidently indented the last definition (numberwritten) slightly but other than that it's correct and it's all contained within the same class: class Window(). The rest I believe it also correct as it works. It's just these few problems I am having.

What happens in __init__, do you have class Window(Tk) or class Window(Frame)?
How you make the instance of it and enter the mainloop?

I have class Window(): with self.root = Tk() as the main window. I have managed to figure out the button. My only problem here now is getting the 'n' values from each entry box generated. The user provides the number of columns and I then need to get all values per row to use as variables.

Does not function for me:

from Tkinter import *

class Window():
    def __init__(self):
        self.root = Tk()

    def ValueBox(self):
        self.number_boxes = self.value.get()
        self.win2.destroy
        return self.number_boxes

    def ChoiceBox(self, choice):
        if choice == "List":
            self.win2 = Tk()
            self.win2.title("List")
            self.list_text = Label(self.win2, text="Please enter number of values to be used:")
            self.list_text.grid(row=0, column=0, sticky="nsew", padx=1, pady=1)
            self.value = StringVar()
            self.list_values = Entry(self.win2, textvariable=self.value, justify="center")
            self.list_values.grid(row=1, column=1, sticky="nsew", padx=1, pady=1)
            #self.value_number = self.value.get()

            #self.list_button = ttk.Button(self.win2, text="Enter", command=self.ValueBox)
            self.list_values.bind('<Return>', self.ValueBox) # Link entry button to box.
            #self.list_button.grid(row=2, column=1, sticky="nsew", padx=1, pady=1)
            self.win2.mainloop()

            #self.number_boxes = 4
            column = 7

            self.numbers = [StringVar() for i in xrange(self.number_boxes) ] #Name available in global scope. 

            for i in xrange(self.number_boxes): 
                choice_title = Label(self.frame_table, text='Value %g'% float(i+1), bg='white', borderwidth=0, width=10) 
                choice_title.grid(row=1, column=column+i, sticky="nsew", padx=1, pady=1) 
                boxes=[]

            for i in xrange(self.number_boxes):
                for j in range(2, rows): 
                    box=Entry(self.frame_table,bg='white',borderwidth=0,textvariable=self.numbers[i], width=10, justify="center") 
                    box.grid(row=j,column=column+i, sticky='nsew', padx=1, pady=1) 
                    boxes.append(box)
                    print boxes
                    # = map(float, co_ord.strip('()').split(','))
            #box1,box2,box3,box4=boxes


        for i in self.numbers: 
            i.trace('w',lambda a,b,c,n=i: self.numberwritten(n) ) 


    def numberwritten(self, numbers):
        fg = self.numbers.get()
        print fg

w = Window()
w.root.mainloop()

I also have another window as a frame of root, win1 = Frame(self.root), I could post all of my code but since the problem I have with obtained the 'n' values is here, I didn't want to waste space. My problem is with lines 31, 36, 42, 43, 48, 49, 52-54 (in the code you posted above). Depending on the number of rows (2, rows) and columns I need to obtain a list of the values in entry boxes per row.

You have two instances of Tk running which is never a good idea. Use a Toplevel instead. The following is a very simple example. But it will not solve the problems. The problem is that you have somewhere around 100+ lines of code that have not been tested so you have not idea where the problem(s) are. Test each function individually before coding the next function.

class ChoiceTest():
    def __init__(self):
        self.root=tk.Tk()
        self.root.geometry("+5+5")
        tk.Label(self.root, text="\n Main Window \n").grid()
        tk.Button(self.root, text="Exit", command=self.root.quit).grid(row=1, column=0)
        self.choice_box("List")
        self.root.mainloop()

    def choice_box(self, choice):
        if choice == "List":
            self.win2 = tk.Toplevel(self.root)
            self.win2.title("List")
            self.win2.geometry("+100+100")
            self.list_text = tk.Label(self.win2, 
                       text="Please enter number\nof values to be used:")
            self.list_text.grid(row=0, column=0, sticky="nsew", 
                                 padx=1, pady=1)
            self.value = tk.StringVar()
            self.list_values = tk.Entry(self.win2, 
                        textvariable=self.value, justify="center")
            self.list_values.grid(row=1, column=0, sticky="nsew", padx=1, pady=1)
            tk.Button(self.win2, text="Continue", 
                      command=self.get_value).grid(row=2, column=0)

    def get_value(self):
        print self.value.get()
        self.win2.destroy()

CT=ChoiceTest()

Edited 4 Years Ago by woooee

Comments
solid to the point advice
This article has been dead for over six months. Start a new discussion instead.