我有两个选项菜单,其中一个的内容通过从另一个选择一个值来更新。虽然这是一个相当普遍的情况,但我似乎仍然无法使其发挥作用。我现在拥有它以便选项更新但是从第二个更新的框中选择一个项目时会抛出以下错误:
self.SelectFrame['menu'].add_command(label=frame, command=lambda v = self.varFrame, l=frame:v.set(1))
AttributeError: 'str' object has no attribute 'set'`
有关选项的声明代码如下:
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)
在同一类中的另一个方法是这段代码:
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))
在更新后从菜单中选择一个项目时会抛出错误,我不明白为什么我确信lambda命令可以设置它。如果在没有lambda的情况下删除这个命令,我已经尝试了其他各种方法,但它似乎没有用。
答案 0 :(得分:2)
self.varFrame
不是正常string
,而是StringVar
,您无法通过
self.varFrame = ""
这样您就可以将StringVar
替换为普通string
,现在您无法使用self.varFrame.set()
(错误中为v.set()
)
您必须始终使用set()
self.varFrame.set("")