Reputation: 15
I have two option menus and the contents of one is updated by selecting a value from the other. Whilst this is a fairly common situation I still can't seem to make it work. I now have it so that the options update but upon selecting an item from the second, updated box it throws the following error:
self.SelectFrame['menu'].add_command(label=frame, command=lambda v = self.varFrame, l=frame:v.set(1))
AttributeError: 'str' object has no attribute 'set'`
The code for the declaration of the optionmenu in question is below:
self.varFrame = Tk.StringVar()
self.Frames = ["",""]
self.SelectFrame = Tk.OptionMenu(botLeftFrame, self.varFrame, *self.Frames)
self.varFrame.set("None selected")
self.SelectFrame.pack(side="left", padx = 5, pady = 10)
In another method in the same class is this code:
def runSelectionChanged(self,*args):
cnxn = pyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\Users\Public\dbsDetectorBookingSystem.accdb')
cursor = cnxn.cursor()
cursor.execute("SELECT RunFilePath, RunID FROM tblRuns")
rows = cursor.fetchall()
for row in rows:
if row.RunFilePath == self.varRun.get():
chosenRunID = row.RunID
sqlString = "SELECT LocalFilePath, RunID FROM tblFrames WHERE RunID=?"
cursor.execute(sqlString, str(chosenRunID))
self.userFrames = cursor.fetchall()
self.Frames = ["",""]
for frame in self.userFrames:
self.Frames.append(frame.LocalFilePath)
newFrames = self.Frames
self.varFrame = ""
self.SelectFrame['menu'].delete(0, 'end')
for frame in newFrames:
self.SelectFrame['menu'].add_command(label=frame, command=lambda v = self.varFrame, l=frame:v.set(1))
The error is thrown when you select an item from the menu after it has been updated, I do not understand why as I am sure that the lambda command gives it the ability to be set. I have tried various other ways if phrasing this command without lambda but it doesn't seem to work.
Upvotes: 0
Views: 1169
Reputation: 142641
self.varFrame
is not normal string
but StringVar
and you can't set value by
self.varFrame = ""
This way you replaced StringVar
by normal string
and now you can't use self.varFrame.set()
(in you error it is v.set()
)
You have to always use set()
self.varFrame.set("")
Upvotes: 2