Search code examples
pythonfor-loopvisual-studio-codekivypylance

Variable in for loop not accessed by Pylance


I'm making an application in python that can access an excel file to read and write data. Excel file has weekdays and user will be reading or writing the cell next to them. Below, I wrote some loops to check if user left some changes unsaved. And if the cell for a certain day has unsaved changes, label inside the app changes to point it out to the user.

Like: Monday --> Monday*

"kvlabel", for some reason stays in a darker color and hovering my mouse over it reveals that it's not accessed by Pylance. I changed it's name and position but it doesn't make a difference.

daytexts = [
    self.root.get_screen("weeklywin").ids.mon_text.text, 
    self.root.get_screen("weeklywin").ids.tue_text.text, 
    self.root.get_screen("weeklywin").ids.wed_text.text, 
    self.root.get_screen("weeklywin").ids.thu_text.text, 
    self.root.get_screen("weeklywin").ids.fri_text.text
    ]

cells = [e4, e10, e16, e22, e28]

daylabel = ["mon_label", "tue_label", "wed_label", "thu_label", "fri_label"]

daylabelkv = [
    self.root.get_screen("weeklywin").ids.mon_label.text, 
    self.root.get_screen("weeklywin").ids.tue_label.text, 
    self.root.get_screen("weeklywin").ids.wed_label.text, 
    self.root.get_screen("weeklywin").ids.thu_label.text, 
    self.root.get_screen("weeklywin").ids.fri_label.text
    ]

justday = ["Pazartesi", "Salı", "Çarşamba", "Perşembe", "Cuma"]

for day, cell, label in zip(daytexts, cells, daylabel):
    if day != cell and label not in self.excel_unsaved_label:
        self.excel_unsaved_label.append(label)
    elif day == cell and label in self.excel_unsaved_label:
        self.excel_unsaved_label.remove(label)
        
if self.excel_unsaved_label:
    self.show_excel_leave_warn()
    for label2, kvlabel, tehday in zip(daylabel, daylabelkv, justday):
        if label2 in self.excel_unsaved_label:
            kvlabel = tehday+"*"
        else:
            kvlabel = tehday
                    
else:
    self.root.current = caller

Solution

  • Like @slothrop suggested, I removed .text from daylabelkv:

    daylabelkv = [
        self.root.get_screen("weeklywin").ids.mon_label, 
        self.root.get_screen("weeklywin").ids.tue_label, 
        self.root.get_screen("weeklywin").ids.wed_label, 
        self.root.get_screen("weeklywin").ids.thu_label, 
        self.root.get_screen("weeklywin").ids.fri_label]
    

    and changed the loop like this:

    for label2, i, tehday in zip(daylabel, range(len(daylabelkv)), justday):
        if label2 in self.excel_unsaved_label:
            daylabelkv[i].text = tehday+"*"
    

    It does what I want now. Thank you all for your help.