0

I have two classes in my project. One is the GUI and the other makes the calculations. I read data from two Entry boxes from the first and want to pass them to the second. So far, what I do is this:

class OptimizationGUI:
    land_entry = 0
    pop_entry = 0

    def __init__(self, master):
        self.land_entry_text = IntVar()
        self.pop_entry_text = IntVar()

        self.master = master

        OptimizationGUI.land_entry = Entry(master, textvariable=self.land_entry_text)

        self.advanced_options = Button(master, text="Advanced", command=self.advanced)

        self.run = Button(master, text="RUN", command=self.start, bg='black', fg='red', bd=4)


    def advanced(self):
        advanced_window = Toplevel()
        OptimizationGUI.pop_entry = Entry(advanced_window, textvariable=self.pop_entry_text)

    def start(self):
        opt = Optimization()
        method = getattr(opt,'get_result')
        yo = method()

In the above code, I initiate the two as class variables and then I create the Entries. I go from class OptimizationGui to the other with the getattr. The code of the other class is below:

class Optimization():

    def __init__(self):
        self.population = OptimizationGUI.pop_entry.get()
        self.land = OptimizationGUI.land_entry.get()
        print self.population, self.land

The weird thing is, that while it prints the data of land_entry correctly, when it comes to the self.population = OptimizationGUI.pop_entry.get() line, it prints the following error:

AttributeError: 'int' object has no attribute 'get'

The only difference I see between these two is that the pop_entry variable is not in the init function but in the "advanced". What is the way to overcome this?

tzoukritzou
  • 337
  • 1
  • 4
  • 16

1 Answers1

2

Call the advanced method inside init:

def __init__(self, master):
    ...
    self.advanced()

The difference is that the class variable integer 0 land_entry gets overwritten by an Entry object whereas pop_entry isn't overwritten, as advanced is the part that overwrites it and it is never called.

Nae
  • 14,209
  • 7
  • 52
  • 79
  • That fixed it indeed. However, now when I run the program, the "advanced" window pops up without me clicking the advanced button. Also, sometimes I get this error for the same line: return self.tk.call(self._w, 'get') TclError: invalid command name – tzoukritzou Mar 16 '18 at 08:36
  • @tzoukritzou Then instead of this, make sure to instantiate `Optimization` object some time _after_ `advanced` method is called. More isolatedly, make sure to call `.get()` only sometime _after_ its an object that has the method (`Entry`). – Nae Mar 16 '18 at 08:40
  • @tzoukritzou I think you need to prepare a better algorithm then. – Nae Mar 16 '18 at 10:16