Search code examples
python-2.7tkinteropenpyxloptionmenu

Saving an OptionMenu selection to a variable in Tkinter for python 2.7


I am making a GUI in Tkinter that has the user input information and writes it to an excel via openpyxl. I have a few OptionMenu widgets in there and I want to be able to save whatever the user selects from the dropdown menu to a variable that I can then write to a file. Here is what I have so far:

    self.equipment = StringVar(top)
    self.equipment.set("10077")
    self.e18 = OptionMenu(top, self.equipment,'10077','G2143','G2145','17727')
    self.e18.grid(row=13, column=1, sticky=E+W)

Later, in another function I assign it to a variable:

    equipment = self.equipment

I then write it to a file:

    ws1['D18'] = str(equipment)

When I open the file, instead of the user selected string showing up in the cell, this does: PY_VAR16

All the other information entered in Entryboxes write to excel perfectly. Anybody know how to save the OptionMenu selection to a string so that it will write to excel? Is there an equivalent of a .get() command for this widget? Thanks in advance


Solution

  • Is there an equivalent of a .get() command for this widget?

    According to this page, StringVars do indeed have a get method. I suggest replacing

    ws1['D18'] = str(equipment)
    

    With

    ws1['D18'] = equipment.get()