Question

The following code (without showing it all) is my first attempt at using classes. I'm trying to mainly pass the variable box as I wish to retrieve the values from the Entry widgets in Tkinter.

I seem to have possibly made the class work but I'm having difficulties calling the definitions from inside by the 'command' in the Tkinter widgets.

If someone could explain where my badly written class code is going wrong and whether or not getting the box values is possible I would be very happy.

I apologise if this is a repeat of any previous questions.

class Start():
    def ChoiceBox(self, choice):


        column = 0
        if choice == "Fixed":
            choice_frame.grid_forget()      
            tkMessageBox.showinfo("Message", "No optimisation, value fixed.")
        elif choice == "List":
            i = [0, 1, 2, 3]

            for i in i:
                choice_title = Label(choice_frame, text='Value %g'% float(i+1), bg='white', borderwidth=0, width=0)
                choice_title.grid(row=0, column=column+i, sticky="nsew", padx=1, pady=1)

                self.box = Entry(choice_frame, bg='white', borderwidth=0, width=0)
                self.box.grid(row=1, column=column+i, sticky="ew", padx=1, pady=1)

                tkMessageBox.showinfo("Message", "Please fill in list values.")


        elif choice == "Interval" or "Optimisation":
            i = [0, 1]
            choice_title1 = Label(choice_frame, text='Min Value', bg='white', borderwidth=0, width=0)
            choice_title1.grid(row=0, column=column, sticky="nsew", padx=1, pady=1)
            choice_title2 = Label(choice_frame, text='Max Value', bg='white', borderwidth=0, width=0)
            choice_title2.grid(row=0, column=column+1, sticky="nsew", padx=1, pady=1)
            for i in i:
                box = Entry(choice_frame, bg='white', borderwidth=0, width=0)
                box.grid(row=1, column=column+i, sticky="nsew", padx=1, pady=1)
                tkMessageBox.showinfo("Message", "Enter Min/Max values.")


    def StartBut(self):
    if self.box.get() == "":
            pass
        else:
            value = self.box.get()

list1 = OptionMenu(frame_table, variablelist, 'Fixed', 'List', 'Interval', 'Optimisation', command=Start.ChoiceBox)

but1 = Button(frame_but, text='Start', command=Start.StartBut)

UPDATE: Error I am now getting: 'unbound method 'ChoiceBox()' must be called with Start instance as first argument (got str instance instead).

Was it helpful?

Solution

def ChoiceBox(choice): this should be def ChoiceBox(self, choice):

read more about how python classes work here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top