Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is a time tracker app and it should track everything ( even open tabs on chrome ). now first i wanted to use win32gui but i was getting an error and i couldn't fix it ( Getting requirements to build wheel ... error - error: subprocess-exited-with-error ), so i installed pywin32 but now when i run the app it says:
argument of type 'NoneType' is not iterable.
File "D:\ATT.py", line 58, in <module>
if 'Google Chrome' in new_window_name:

why am i getting this error?
what should i do to fix it?

Code:
Python
from __future__ import print_function
import time
from os import system
from activity import *
import json
import datetime
import sys
if sys.platform in ['Windows', 'win32api', 'cygwin']:
    import pywin32
    import uiautomation as auto

active_window_name = ""
activity_name = ""
start_time = datetime.datetime.now()
activeList = AcitivyList([])
first_time = True


def url_to_name(url):
    string_list = url.split('/')
    return string_list[2]


def get_active_window():
    _active_window_name = None
    if sys.platform in ['Windows', 'win32api', 'cygwin']:
        window = pywin32.GetForegroundWindow()
        _active_window_name = pywin32.GetWindowText(window)
    else:
        print("sys.platform={platform} is not supported."
              .format(platform=sys.platform))
        print(sys.version)
    return _active_window_name

def get_chrome_url():
    if sys.platform in ['Windows', 'win32api', 'cygwin']:
        window = pywin32.GetForegroundWindow()
        chromeControl = auto.ControlFromHandle(window)
        edit = chromeControl.EditControl()
        return 'https://' + edit.GetValuePattern().Value
    else:
        print("sys.platform={platform} is not supported."
              .format(platform=sys.platform))
        print(sys.version)
    return _active_window_name

try:
    activeList.initialize_me()
except Exception:
    print('No json')


try:
    while True:
        previous_site = ""
        if sys.platform not in ['linux', 'linux2']:
            new_window_name = get_active_window()
            if 'Google Chrome' in new_window_name:
                new_window_name = url_to_name(get_chrome_url())
        
        if active_window_name != new_window_name:
            print(active_window_name)
            activity_name = active_window_name

            if not first_time:
                end_time = datetime.datetime.now()
                time_entry = TimeEntry(start_time, end_time, 0, 0, 0, 0)
                time_entry._get_specific_times()

                exists = False
                for activity in activeList.activities:
                    if activity.name == activity_name:
                        exists = True
                        activity.time_entries.append(time_entry)

                if not exists:
                    activity = Activity(activity_name, [time_entry])
                    activeList.activities.append(activity)
                with open('activities.json', 'w') as json_file:
                    json.dump(activeList.serialize(), json_file,
                              indent=4, sort_keys=True)
                    start_time = datetime.datetime.now()
            first_time = False
            active_window_name = new_window_name

        time.sleep(1)
    
except KeyboardInterrupt:
    with open('activities.json', 'w') as json_file:
        json.dump(activeList.serialize(), json_file, indent=4, sort_keys=True)


What I have tried:

I tried using pywin32 and pypiwin32 but it's still not working and i'm getting an error.
Posted
Updated 11-Aug-22 21:57pm

1 solution

Look at the error message, it gives you a lot of information:
text>argument
if 'Google Chrome' in new_window_name:
It tells you which file, and what line: "ATT.py", line 58:
Python
if 'Google Chrome' in new_window_name:
It tells you what the problem is:
argument of type 'NoneType' is not iterable

So, the in operator expects a collection - understandably - but new_window_name doesn't hold a collection, it contains NoneType instead.

So look at the line which loads new_window_name:
Python
new_window_name = get_active_window()
and that means that the method call doesn't return a collection. So look at the method, and use the debugger to find out why.

We can't do that for you, as it needs your code running on your system with your data in order to start working out where the None is coming from.
 
Share this answer
 
Comments
brandon1999 12-Aug-22 4:06am    
Thank you. fixed it.
OriginalGriff 12-Aug-22 4:37am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900